Pydantic AI

Trace a Pydantic AI agent with Neatlogs (Python).

Wrap a Pydantic AI Agent with neatlogs.wrap() and its runs — model calls and tool calls — are captured.

Prerequisites

pip install -U "neatlogs[pydantic-ai]" pydantic-ai

Usage

import os
import neatlogs

neatlogs.init(api_key=os.environ["NEATLOGS_API_KEY"], workflow_name="pydantic-ai-demo")

from pydantic_ai import Agent

agent = neatlogs.wrap(Agent("openai:gpt-4o", system_prompt="Be concise."))

result = agent.run_sync("In one sentence, what is Pydantic AI?")
print(result.output)

neatlogs.flush()
neatlogs.shutdown()

The agent run becomes an AGENT trace root automatically, with nested LLM and TOOL spans — no manual WORKFLOW wrapper needed. Add one only to group several agent runs into a single trace.

Sessions & end-user

Say your Pydantic AI agent runs a handful of times over one back-and-forth and you want all of it filed under a single session, credited to the person you're serving — Neatlogs handles that. Wrap the run in identify() and the root the Agent opens picks up the session and end-user from that block:

# Same session_id every turn → one session; end_user_id attributes it to your user.
with neatlogs.identify(session_id=f"conv_{conversation_id}", end_user_id=user_id):
    result = agent.run_sync(message)

Not every run is a conversation. A standalone workflow — a one-off job, not a back-and-forth — is a single trace with no turns to group. You still attribute it to the customer it ran for, sourcing the id and any metadata from your own user or request object:

Warning

A standalone workflow must not carry a session_id. Omit it entirely — Neatlogs sets session_id = trace_id, so the run is its own single-turn session, fully attributed to the end-user. Reusing one session_id across unrelated runs wrongly folds them into a single multi-turn conversation.

def summarize(document, user):
    # Standalone workflow — attribute to the end-user; do NOT pass session_id.
    with neatlogs.identify(
        end_user_id=str(user.id),
        end_user_metadata={"plan": user.plan},
    ):
        return agent.run_sync(f"Summarize:\n{document}").output

Sessions and End-User Identity walk through the whole model.

On this page

Ask Neatlogs AI

Answers from the docs

How can I help?

Ask anything about instrumenting, tracing, or the Neatlogs dashboard.