Skip to content

dbirks/agent-framework-patterns

Repository files navigation

Agent framework patterns

A collection of example scripts showing some common patterns in AI agent frameworks, here specifically using PydanticAI. The concepts are mostly all transferable to other frameworks though, hopefully making this a quick intro to agent frameworks in general.

Prerequisites

Install uv

This project uses uv for dependency management and script execution.

macOS:

brew install uv

Windows:

winget install astral-sh.uv

Configure environment variables

Copy .env.example to .env and add your API keys:

cp .env.example .env

Edit .env and add your API keys:

# Get your API keys from:
# Anthropic: https://console.anthropic.com/settings/keys
ANTHROPIC_API_KEY=sk-ant-...

# OpenAI: https://platform.openai.com/settings/organization/api-keys
OPENAI_API_KEY=sk-proj-...

# Google: https://console.cloud.google.com/apis/credentials
GOOGLE_API_KEY=ABC123...

# Choose your model
MODEL=anthropic:claude-haiku-4-5

Running Scripts

Each script is self-contained with its own dependencies. Simply run:

./01_hello.py

Or explicitly with uv:

uv run 01_hello.py

Scripts

01_hello.py - Hello world

Creates an agent that answers a single question with one sentence.

02_tool_call.py - Using tools

Shows how to define custom tools with @agent.tool_plain. Agent uses a dice rolling tool to generate random numbers.

03_multiple_tool_calls.py - Multiple tool calls

Agent calls a weather API tool multiple times to compare temperatures across Tokyo, Sydney, and London. Includes Logfire observability for tracking tool calls.

04_structured_outputs.py - Structured Outputs

Agent fetches weather data for 13 cities worldwide and returns a typed response, displayed in a table.

05_multi_agent.py - Agents calling other agents

A coordinator agent delegates to two specialized sub-agents: a research agent that searches DuckDuckGo for quantum computing information, and a writing agent that transforms findings into prose.

06_using_mcp_tools.py - Using tools from a MCP Server

Connects to a Kubernetes MCP server and uses its tools to check deployment status. Shows how to integrate external tool servers using Model Context Protocol.

07_conversation_history.py - Conversation history

Interactive travel planning assistant that maintains context across multiple turns by passing message_history between runs. The agent asks questions and remembers your answers.

08_llm_as_judge.py - LLM as Judge

A writer agent creates LinkedIn posts, and a judge agent (Claude Haiku) validates them against criteria.

09_human_in_the_loop.py - Human-in-the-Loop

Multi-agent goat negotiation where your negotiator agent works with you to buy goats from an unpredictable seller agent. The agents barter among themselves, but the human approves each final counteroffer before it's sent.

About

A collection of AI agent framework patterns

Topics

Resources

Stars

Watchers

Forks

Languages