1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
29 small wins to finish your pathNext lesson →
Agent basics and a first run
An Agent is an object that holds a model and its instructions; run_sync sends it a prompt and returns the answer.
Last updated: 28 Sep, 2026 · Pydantic AI 2.51
After the setup in Installation and setup, you can run an agent with no provider at all. The built-in test model answers with a fixed string, so you see the shape of a run before a real model is involved.
Creating an agent and running it
Two lines make an agent and send it a ticket:
from pydantic_ai import Agent
agent = Agent("test", instructions="You answer customer support tickets.")
result = agent.run_sync("I was charged twice for one order")Running the test model end to end
from pydantic_ai import Agent
agent = Agent("test", instructions="You answer customer support tickets.")
result = agent.run_sync("I was charged twice for one order")
print(result.output)
print(result.usage)Output
success (no tool calls) RunUsage(input_tokens=57, output_tokens=4, requests=1)
What the run returned
- result.output is the answer. The test model never reads the prompt, so the text is fixed at
success (no tool calls). - result.usage counts the run:
requests=1means the agent called the model once. - The token counts are estimated by counting words, not billed tokens, so they are plausible rather than exact.
Choosing run_sync, run or run_stream
agent.run_sync(prompt)waits for the answer. Use it in scripts.await agent.run(prompt)is the async version, for web servers and handling several tickets at once.agent.run_stream(prompt)gives the answer piece by piece, covered later in the course.
Running an agent asynchronously
import asyncio
async def main():
result = await agent.run("My parcel never arrived")
print(result.output)
asyncio.run(main())Output
success (no tool calls)
The test model vs a hosted model
| Test model | Hosted model | |
|---|---|---|
| Reads the prompt | No, returns a fixed answer | Yes |
| Needs a key | No | Yes |
| Token counts | Estimated from words | Billed by the provider |
| Use for | Seeing a run, wiring, tests | Real answers |
The first time an agent runs in a process, Pydantic AI prints a start-up banner to standard error with its version and what the agent holds. It stays out of standard output, so piping a run into a file leaves it out. Set PYDANTIC_AI_NO_BANNER=1 in the environment to silence it.
Create agents once
Watch out. An agent is meant to be created once at the top of a module and reused for every request, the way a web app object is. Nothing about a single run is stored on it, so building a new agent per request wastes work.
Related
- Previous: Installation and setup
- Next: Requests and responses in a run
- Reference: Agents
Try it yourself
- Change the prompt and run again. Does the output change?
- Print
result.usage.requestsandresult.usage.input_tokens. - Run the agent twice in one script; set
PYDANTIC_AI_NO_BANNER=1and confirm the banner is gone.
You understood something today that you didn't yesterday.