<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/building-an-agent-with-pydantic-ai-uvh89x786" -->

---
title: Building an Agent with Pydantic AI | daily.dev
description: A beginner-friendly walkthrough of building an AI agent using the Pydantic AI library in Python. Covers core concepts including system prompts, instructions,...
canonical: https://daily.dev/posts/building-an-agent-with-pydantic-ai-uvh89x786
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Building an Agent with Pydantic AI | daily.dev
og:description: A beginner-friendly walkthrough of building an AI agent using the Pydantic AI library in Python. Covers core concepts including system prompts, instructions,...
og:url: https://daily.dev/posts/building-an-agent-with-pydantic-ai-uvh89x786
og:image: https://api.daily.dev/og/posts/UvH89X786.png
og:image:alt: Building an Agent with Pydantic AI
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Building an Agent with Pydantic AI

**[Medium](https://daily.dev/sources/medium_js)** · 15 min read · 1 upvotes · 0 comments

## Summary

A beginner-friendly walkthrough of building an AI agent using the Pydantic AI library in Python. Covers core concepts including system prompts, instructions, guardrails, tool assignment (via decorators and constructor), dependency injection via RunContext, typed outputs using Pydantic BaseModel, and the different run methods (async, sync, streaming). A complete carpenter-themed example ties all concepts together, with notes on production considerations like error handling and observability.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://medium.com/@gwils1414/building-an-agent-with-pydantic-ai-6b956d243d73>

## Questions this post answers

### What are the three ways to assign tools to an agent in Pydantic AI?

Pydantic AI offers three ways to assign tools: the @agent.tool() decorator for tools that need access to run context and dependencies, the @agent.tool_plain() decorator for plain functions with no context needed, and passing a list of tool functions directly via the tools= parameter when constructing the Agent instance.

_Compare agent framework patterns like this on daily.dev before choosing how to wire up tools._

### How do I pass dependencies like a database path or user id into a Pydantic AI tool function?

Define a dataclass (for example Deps with db_path and user_id fields), pass it as deps_type when creating the Agent, and supply an instance via the deps= parameter on agent.run(). Inside a tool decorated with @agent.tool(), accept a RunContext[Deps] parameter and read ctx.deps.db_path or ctx.deps.user_id to access the injected values.

_Developers wiring dependency injection into agents can track these patterns on daily.dev._

### What is the difference between agent.run(), run_sync(), run_stream(), and run_stream_sync() in Pydantic AI?

agent.run() is an asynchronous function returning a RunResult; run_sync() is its synchronous version, internally calling loop.run_until_complete(self.run()). run_stream() is an async context manager that streams the response token by token and returns a StreamedRunResult, while run_stream_sync() is the synchronous equivalent of streaming.

_Anyone deciding how to structure agent chat calls can revisit comparisons like this on daily.dev._

## Similar posts on daily.dev

- [Building AI Agents in Python with Pydantic AI](https://daily.dev/posts/building-ai-agents-in-python-with-pydantic-ai-aj0aokeuz) · Machine Learning Mastery · 4 upvotes · 0 comments
- [Pydantic AI: Build Type-Safe LLM Agents in Python – Real Python](https://daily.dev/posts/pydantic-ai-build-type-safe-llm-agents-in-python-real-python-feaotui1h) · Real Python · 0 upvotes · 0 comments
- [How to Build Production-Grade Agents with Pydantic AI](https://daily.dev/posts/how-to-build-production-grade-agents-with-pydantic-ai-qbzy6k9o0) · freeCodeCamp · 1 upvotes · 0 comments
- [Building Type-Safe LLM Agents With Pydantic AI – Real Python](https://daily.dev/posts/building-type-safe-llm-agents-with-pydantic-ai-real-python-anmkilb4a) · Real Python · 1 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#llm](https://daily.dev/tags/llm), [#ai-agents](https://daily.dev/tags/ai-agents)

[View this post on daily.dev](https://daily.dev/posts/building-an-agent-with-pydantic-ai-uvh89x786)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Building an Agent with Pydantic AI","url":"https://daily.dev/posts/building-an-agent-with-pydantic-ai-uvh89x786","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/building-an-agent-with-pydantic-ai-uvh89x786"},"datePublished":"2026-07-23T22:10:25.376Z","dateModified":"2026-09-14T08:03:08.003Z","description":"A beginner-friendly walkthrough of building an AI agent using the Pydantic AI library in Python. Covers core concepts including system prompts, instructions,...","image":"https://media.daily.dev/image/upload/s--VDukGCjf--/f_auto/v1722860399/public/Placeholder%2002","thumbnailUrl":"https://media.daily.dev/image/upload/s--VDukGCjf--/f_auto/v1722860399/public/Placeholder%2002","isAccessibleForFree":true,"articleSection":"Medium","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Medium","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/medium","url":"https://daily.dev/sources/medium_js"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/building-an-agent-with-pydantic-ai-uvh89x786","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,llm,ai-agents","timeRequired":"PT15M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Medium","item":"https://daily.dev/sources/medium_js"},{"@type":"ListItem","position":3,"name":"Building an Agent with Pydantic AI"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/building-an-agent-with-pydantic-ai-uvh89x786#faq","mainEntity":[{"@type":"Question","name":"What are the three ways to assign tools to an agent in Pydantic AI?","acceptedAnswer":{"@type":"Answer","text":"Pydantic AI offers three ways to assign tools: the @agent.tool() decorator for tools that need access to run context and dependencies, the @agent.tool_plain() decorator for plain functions with no context needed, and passing a list of tool functions directly via the tools= parameter when constructing the Agent instance. Compare agent framework patterns like this on daily.dev before choosing how to wire up tools."}},{"@type":"Question","name":"How do I pass dependencies like a database path or user id into a Pydantic AI tool function?","acceptedAnswer":{"@type":"Answer","text":"Define a dataclass (for example Deps with db_path and user_id fields), pass it as deps_type when creating the Agent, and supply an instance via the deps= parameter on agent.run(). Inside a tool decorated with @agent.tool(), accept a RunContext[Deps] parameter and read ctx.deps.db_path or ctx.deps.user_id to access the injected values. Developers wiring dependency injection into agents can track these patterns on daily.dev."}},{"@type":"Question","name":"What is the difference between agent.run(), run_sync(), run_stream(), and run_stream_sync() in Pydantic AI?","acceptedAnswer":{"@type":"Answer","text":"agent.run() is an asynchronous function returning a RunResult; run_sync() is its synchronous version, internally calling loop.run_until_complete(self.run()). run_stream() is an async context manager that streams the response token by token and returns a StreamedRunResult, while run_stream_sync() is the synchronous equivalent of streaming. Anyone deciding how to structure agent chat calls can revisit comparisons like this on daily.dev."}}]}
```

