/Pydantic AI

A production-ready full-stack template for AI agents with Pydantic AI and Logfire

Antoni Kozelski avatar
Antoni Kozelski
3 mins

Every AI application needs the same foundations: authentication logic, real-time streaming, conversation persistence (handled by a database), observability, deployment, and CI/CD pipelines. These are essential building blocks.

And yet weeks are spent on scaffolding before you can write a single line of business logic.

This Pydantic AI and Pydantic Logfire backed open-source project sets out to fix that chokepoint once and for all. Meet the full-stack-ai-agent-template, an open-source CLI tool that generates production-ready full-stack applications combining FastAPI, Next.js 15, and your choice of Pydantic AI or LangChain for AI agents. One command gives you WebSocket streaming, JWT authentication, database setup with Alembic migrations, Redis caching, Logfire tracing, and Docker deployment.

Here is our breakdown of an all too common chokepoint, and how we integrated Pydantic AI and Pydantic Logfire into the open source. You can read the full write-up on our production-ready template for AI/LLM applications, but here are the highlights.

Building an AI application is not just about wrapping an OpenAI or Anthropic API. When a user asks your Pydantic AI powered chatbot a question, you need to:

  1. Authenticate the request: JWT validation, refresh tokens, or API keys.
  2. Stream the response: WebSocket connections so users see text as it generates.
  3. Handle tool calls: modern agents using Pydantic AI or LangChain call tools, and users need transparency.
  4. Persist conversations: PostgreSQL or MongoDB storage so users continue where they left off.
  5. Monitor everything: Logfire traces, Sentry errors, and Prometheus metrics for production visibility.

Each requirement demands infrastructure. JWT authentication means secure password hashing with bcrypt, token generation, refresh logic, and HTTP-only cookies. WebSocket streaming means connection lifecycle management, graceful disconnection handling, and event serialization. Database persistence means SQLAlchemy models, Alembic migrations, and the repository pattern. Observability means OpenTelemetry instrumentation across FastAPI, asyncpg, Redis, and Pydantic AI.

Teams can spend three months building this scaffold before writing their first AI feature. Startups burn runway on solving these problems. Enterprises pay consulting fees for boilerplate code that every project needs.

This was the pain we set out to eliminate with open-source tooling.

Both frameworks are supported because they serve different needs.

Pydantic AI is our recommended choice for new projects. Built by the Pydantic team, it offers full type safety with Pyright strict mode, native Logfire integration for observability, and a simpler mental model than graph-based frameworks. Its iter() method provides first-class streaming support with access to every event the model produces: text deltas, tool calls, and results.

LangChain makes sense when you need its ecosystem: the extensive tool library, LangSmith for tracing and evaluation, or familiarity for teams already using it.

The generated code structure is identical for both. Your FastAPI routes and business logic stay the same regardless of whether you choose Pydantic AI or LangChain.

Pydantic Logfire is integrated throughout the stack because debugging production AI applications is hard. When a user reports "the chatbot gave a weird answer," you need to see exactly what happened.

The FastAPI lifespan handler configures instrumentation for every subsystem: FastAPI requests with full request and response bodies, PostgreSQL queries via asyncpg with SQL and parameters, Redis cache operations, external HTTP calls via HTTPX, and Pydantic AI agent reasoning including tool calls and model responses.

Every request gets a unique ID that flows through logs, traces, and error reports. When something fails, you see the complete picture: the user's message, the Pydantic AI agent's reasoning, the tool calls, the PostgreSQL queries, and the exact point of failure.

The full-stack-ai-agent-template helps you skip solving the same problems over and over, so you can focus on what matters: your unique AI product.