building-pydantic-ai-agents
Build AI agents with Pydantic AI — tools, capabilities (including on-demand loading), structured output, streaming, testing, and multi-agent patterns. Use when the user mentions Pydantic AI, imports pydantic_ai, or asks to build an AI agent, add tools/capabilities, defer capability loading, stream output, define agents from YAML, or test agent behavior.
Security Assessment
About building-pydantic-ai-agents
A patterns-and-examples guide for building AI agents with Pydantic AI, a Python agent framework for production-grade generative AI applications. It is intended for when a user mentions Pydantic AI, imports pydantic_ai, or wants to build an agent, add tools or capabilities, defer capability loading, stream output, define agents from YAML, or test agent behavior, and explicitly not for the Pydantic validation library alone or other frameworks such as LangChain, LlamaIndex, CrewAI, or AutoGen. It requires Python 3.10 or newer.
The skill provides tested quick-start patterns covering the core building blocks. These include creating a basic Agent with instructions, adding tools through the tool_plain and tool decorators with a RunContext, producing structured output by passing a Pydantic BaseModel as output_type, injecting dependencies via deps_type and instruction functions, and unit testing with TestModel using the agent's override context. It introduces capabilities as reusable, composable units of agent behavior that bundle tools, hooks, instructions, and model settings (for example Thinking and WebSearch), lifecycle Hooks that intercept model requests, tool calls, and runs through decorators without subclassing, and loading agents from YAML or JSON specs with Agent.from_file. A recurring theme is progressive disclosure, deciding what should load eagerly versus on demand and deferring capability loading for optional instructions, specialist workflows, and long-tail tools the model does not need on most turns.
Rather than inlining everything, the skill uses a task routing table that points to focused references such as Agents Core, Capabilities and Hooks, Capabilities on Demand, Tools Core, Native Tools, Tools Advanced, Input and History, Testing and Debugging, Orchestration and Integrations, and an Architecture and Decision Guide, with guidance to load only the most relevant reference first. Advanced topics span function tools, toolsets, MCP servers, native web search and code execution, multimodal input and message history, multi-agent coordination and graph workflows, durable execution, embeddings, evals, and observability with Logfire.
FAQ
What is this skill for and what is it not for?
It is for building AI agents with Pydantic AI, including tools, capabilities, structured output, streaming, testing, and multi-agent patterns. It is not for the Pydantic validation library alone or for other frameworks like LangChain, LlamaIndex, CrewAI, or AutoGen.
What are the requirements?
It requires Python 3.10 or newer and the pydantic_ai package, since the patterns import from pydantic_ai and reference classes such as Agent, RunContext, and Tool.
How do you add tools to an agent?
Using the @agent.tool_plain decorator for tools that do not need context and @agent.tool for tools that take a RunContext to access dependencies, both documented with runnable examples.
Can agents be defined without writing Python construction code?
Yes. Agent.from_file loads an agent from a YAML or JSON spec, where the model, instructions, and capabilities (such as WebSearch and Thinking) are declared declaratively.
How is agent behavior tested?
With TestModel from pydantic_ai.models.test, used inside the agent's override context so a run can be asserted on without calling a real model, including checking that no tool calls were made.
All Files
12 filesInstall building-pydantic-ai-agents
Quick Setup:
- Copy the skill folder to
.claude/skills/ - Claude will automatically detect and use the skill
Repository
pydantic/skills