The LangSmith tracing equivalent

This lesson assumes you've done LangSmith Lessons 1-4, @traceable, tracing basics, run types, and alternative tracing methods, since Logfire is answering the exact same "what actually happened in this run" question.

The LangSmith course wired up tracing by setting environment variables and letting every LangChain call get picked up automatically. Pydantic AI's observability story is built on the same underlying standard, OpenTelemetry, via Pydantic's own logfire package, which you can point at Logfire's hosted UI, or, as this lesson does, keep entirely local by printing spans straight to your console.

Instrumenting an agent

Two calls turn tracing on: configure logfire itself, then tell it to instrument Pydantic AI specifically.

import logfire
from dotenv import load_dotenv
from pydantic_ai import Agent
load_dotenv()
# local-only tracing: prints spans to the console, sends nothing anywhere.
logfire.configure(send_to_logfire=False)
logfire.instrument_pydantic_ai()
agent = Agent("google:gemini-3.5-flash-lite")
@agent.tool_plain
def add(a: int, b: int) -> int:
"""Add two integers together."""
return a + b
def main() -> None:
result = agent.run_sync("What is 12 plus 30? Use the tool.")
print("\nOutput:", result.output)

With that in place, every agent run emits a span, agent run, with nested child spans for each model call, chat gemini-3.5-flash-lite, and each tool invocation, printed to the console as they happen. This is the same "see the whole run's tree of calls" value LangSmith's trace view provides, just rendered locally instead of on a hosted dashboard.

Going further: a real Logfire project

Flipping send_to_logfire=False to True, and setting a LOGFIRE_TOKEN, sends the exact same spans to Logfire's hosted UI instead of your console, searchable, filterable, with cost and latency breakdowns per run. Nothing about the instrumentation call changes, only the destination. That's the same shape as LangSmith's LANGSMITH_TRACING=true switch: local development doesn't require the hosted service, but the same code scales up to it without modification.

Checkpoint

  • logfire.configure(send_to_logfire=False) plus logfire.instrument_pydantic_ai() traces every agent run to your console, no account needed.
  • Spans nest: one agent run span per run_sync call, with child spans for model calls and tool invocations.
  • This is the LangSmith tracing equivalent, built on OpenTelemetry instead of a LangChain-specific integration.

If anything here still feels unclear, ask before moving to Lesson 19, this tier's checkpoint project.