CopilotKit <> PydanticAI Todo App
October 1, 2025 · View on GitHub
A hands-on experiment with the new AG-UI protocol that bridges PydanticAI Python agents with CopilotKit React UIs.
This protocol is genuinely interesting because it solves a real problem: getting Python AI agents to work seamlessly with modern web UIs. Instead of building REST APIs or dealing with websockets manually, AG-UI gives you bidirectional state synchronization out of the box. Your Python agent updates state, and your React UI reflects it automatically—and vice versa.
The demo is a todo board with three columns (Todo, In-Progress, Done), but the real point is to experiment with the protocol itself and see how it works.
Prerequisites
- OpenAI API Key (for the PydanticAI agent)
- Python 3.12+
- uv
- Node.js 20+
- Any of the following package managers:
- pnpm (recommended)
- npm
- yarn
- bun
Getting Started
- Install dependencies using your preferred package manager:
# Using pnpm (recommended)
pnpm install
# Using npm
npm install
# Using yarn
yarn install
# Using bun
bun install
Note: This will automatically setup the Python environment as well.
If you have manual issues, you can run:
npm run install:agent
- Set up your API keys:
Create a .env file inside the agent folder with the following content:
# Required: Get from https://platform.openai.com/api-keys
OPENAI_API_KEY=sk-...your-openai-key-here...
# Optional: For agent observability
# Sign up at https://logfire.pydantic.dev
# Create token: https://logfire.pydantic.dev/docs/how-to-guides/create-write-tokens/
LOGFIRE_TOKEN=...your-logfire-token...
- Start the development server:
# Using pnpm
pnpm dev
# Using npm
npm run dev
# Using yarn
yarn dev
# Using bun
bun run dev
This starts both the UI and agent servers. Open your browser and try interacting with the chat to see the protocol in action.
How It Works
The AG-UI protocol connects three pieces to create a full-stack agentic application:
The Stack
-
PydanticAI (
agent/src/agent.py) - Python AI agent framework- Defines the agent with GPT-4.1-mini model and tools
- Tools (
agent/src/tools.py):add_todos,update_todo,delete_todos, etc. - Uses Pydantic models (
agent/src/models.py) for type-safe state
-
AG-UI Protocol (
agent/src/main.py) - The bridge layer- One line (
agent.to_ag_ui()) turns your PydanticAI agent into a FastAPI server (port 8000) - Handles bidirectional state synchronization via
StateSnapshotEvent - This is the interesting part—it's what makes Python ↔ React communication work without manual API building
- One line (
-
CopilotKit (
src/app/page.tsx,src/app/api/copilotkit/route.ts) - React chat UIuseCoAgent()creates bidirectional state sync with backendHttpAgentconnects to Python AG-UI server- Renders tool calls with custom UI components
🔍 Technical Deep Dive ->
Architecture
graph TD
A[User Input] --> B[CopilotKit UI<br/>src/app/page.tsx]
B --> C[useCoAgent Hook<br/>State Sync]
C --> D[Next.js API Route<br/>src/app/api/copilotkit/route.ts]
D --> E[HttpAgent<br/>:8000 proxy]
E --> F[AG-UI Server<br/>agent/src/main.py]
F --> G[PydanticAI Agent<br/>agent/src/agent.py]
G --> H[Tools<br/>agent/src/tools.py]
H --> I[StateSnapshotEvent]
I --> F
F --> E
E --> D
D --> C
C --> B
B --> J[UI Update]
style A fill:#e1f5ff
style J fill:#e1f5ff
style B fill:#a8daff
style C fill:#a8daff
style D fill:#a8daff
style F fill:#ffcba8
style G fill:#ffcba8
style H fill:#ffcba8
style I fill:#ffd700
Data Flow
Here's what happens when you interact with the chat:
- User types in chat → CopilotKit sends to
/api/copilotkit - Next.js proxies via
HttpAgentto Python backend (localhost:8000) - PydanticAI agent calls tools (e.g.,
add_todos) - Tools return
StateSnapshotEventwith updated state - AG-UI pushes state back through CopilotKit
useCoAgentupdates React UI automatically
The key insight is step 4-6: instead of building REST endpoints, the agent just returns state changes and the protocol handles syncing them to the UI.
Key Files
src/app/page.tsx- CopilotKit integration & state syncsrc/app/api/copilotkit/route.ts- Proxy to Python backendagent/src/agent.py- PydanticAI agent definitionagent/src/tools.py- Todo management toolsagent/src/models.py- Shared data modelsagent/src/main.py- AG-UI server setup
Resources
Documentation
- AG-UI Protocol Documentation - The core protocol this project demonstrates
- PydanticAI Documentation - Python AI agent framework
- CopilotKit Documentation - React UI framework
- Next.js Documentation - Frontend framework
Available Scripts
The following scripts can also be run using your preferred package manager:
dev- Starts both UI and agent servers in development modedev:debug- Starts development servers with debug logging enableddev:ui- Starts only the Next.js UI serverdev:agent- Starts only the PydanticAI agent serverbuild- Builds the Next.js application for productionstart- Starts the production serverlint- Runs ESLint for code lintinginstall:agent- Installs Python dependencies for the agent
About This Project
This project explores the AG-UI protocol, which takes a different approach to connecting Python AI agents with web UIs.
What's interesting about it:
- Bidirectional by default: State flows between Python and React without writing REST endpoints or GraphQL schemas
- Minimal setup:
agent.to_ag_ui()turns your PydanticAI agent into a server - Type-safe: Pydantic models work across the stack
- Agent-first: Designed for workflows where the AI makes decisions and updates state
Things to try:
- Modify the tools in
agent/src/tools.pyto add new agent capabilities - Add new Pydantic models in
agent/src/models.pyand see how they propagate - Check out how
StateSnapshotEventhandles state synchronization - Use this as a starting point for your own agentic application
The todo app is just a demo—the protocol itself is what's worth exploring.
License
This project is licensed under the MIT License - see the LICENSE file for details but TLDR is do whatever you want.
Troubleshooting
Agent Connection Issues
If you see "I'm having trouble connecting to my tools", make sure:
- The PydanticAI agent is running on port 8000
- Your OpenAI API key is set correctly
- Both servers started successfully
Python Dependencies
If you encounter Python import errors:
cd agent
uv sync
uv run src/main.py