翻篇收录说明
翻篇网页快照

Pydantic AI Harness | Pydantic Docs

https://pydantic.dev/docs/ai/harness/

最近抓取:2026-12-05 18:46:06(北京时间)

最近内容变化:2026-12-03 06:42:13(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Pydantic AI Harness is the official capability library for Pydantic AI: a coding agent, file and shell tools, memory, subagents, and context management.

正文摘录

Pydantic AI Harness Your agent’s favorite harness, built on Pydantic AI Pydantic AI Harness is the official capability and harness library for Pydantic AI. Every Pydantic AI agent already has a light harness: the typed agent loop, any model, your own tools, structured output. For simple agents that’s enough. But set an agent loose on complex, long-running work (fix a codebase, research a question, run for hours unattended) and what it needs around the model grows: a workspace to act in, a plan it keeps current, memory that carries across sessions, sub-agents to hand work to, context management for long runs, and durable execution that survives a restart. Pydantic AI Harness ships that harness. Everything here is one primitive: a capability, a self-contained unit of agent behavior you add to capabilities=[...] on any agent. There are 100+ of them, and complete agents like Coder and Researcher are themselves combinations of capabilities. Use a single capability, compose your own set, or start from the whole coding agent and swap parts out. Pydantic AI, the harness SDK, Coder, and Pax An agent on its own is a loop calling a model and tools. A harness is everything around that loop that makes it hold up on real work: the workspace it acts in, the tools, context management, recovery, guardrails, and the interface you steer it through. Four layers carry that idea; start with the one that matches what you are doing: LayerWhat it isStart here whenPydantic AI (pydantic-ai)The agent framework: the agent loop, models from every major provider, tools, structured output, and the capability systemYou are building an agent, or an LLM feature in your own applicationPydantic AI Harness (pydantic-ai-harness)The SDK you build a harness of your own from: workspaces, file and shell tools, memory, sub-agents, context management, and ready-made agents like CoderYour agent needs to edit files, run commands, remember, delegate, or keep working through long runsCoderA ready-made coding agent in one capability: file and shell tools, task delegation, repo instructions and context management. You supply the workspace it runs in, and any guardrailsYou want a coding agent in your own code, to run, extend or swap parts ofPax (pydantic-ai-pax)The TUI in front of Coder, and a terminal interface for any Pydantic AI agent; everything in it is a plugin, the coding agent includedYou want to use a coding agent rather than build one, or run your own agent in a terminal Each package installs the one it is built on: pydantic-ai-harness installs pydantic-ai-slim, and pydantic-ai-pax installs pydantic-ai-harness. Coder supplies the tools, context management and recovery; you choose the workspace it runs in and any guardrails, and Pax is the interface in front of it. That is why either works without the other: build on Coder and never open Pax, or point Pax at an agent that is entirely your own. Neither is clai, the legacy CLI that ships with Pydantic AI. Quick start pip uv Terminalpip install "pydantic-ai-harness[coder,anthropic]" from pydantic_ai import Agent from pydantic_ai.capabilities import LocalWorkspace from pydantic_ai_harness.coder import Coder agent = Agent( 'anthropic:claude-opus-5-5', capabilities=[LocalWorkspace('.'), Coder()], ) result = agent.run_sync('Find out why tests/test_parser.py fails and fix the bug it caught.') print(result.output) LocalWorkspace('.') is where the agent works: its file tools and commands run on your machine, in this directory. It is not a sandbox, so commands can reach anything you can. To run the same agent in an isolated cloud machine, swap it for a sandbox capability (Modal, E2B, Render, Sprites, Daytona, or boxd); see Workspaces. With Modal, for example: from pydantic_ai_harness.modal_sandbox import ModalSandbox agent = Agent('anthropic:claude-opus-5-5', capabilities=[ModalSandbox(), Coder()]) Coder provides six tools: read_file, write_file, edit_file, list_files, grep, and shell, plus delegate_task to hand a sub-task to a fresh run of the same agent, repository context, and context controls. Shell commands are unrestricted and can persist beyond individual runs. Default instructions guide autonomous investigation, editing, and verification; pass instructions= to add your own guidance. Terminaluv tool install pydantic-ai-pax pax Point it at your own agent with pax --agent mymodule:agent, or at the bundled export with pax --agent pydantic_ai_harness.coder:coder_agent -m anthropic:claude-opus-5-5, which is the example above without a model. Every model works: swap the string for any provider’s. Need more? Add capabilities to the list; here’s the same coder on gpt-6.1-sol, with web search and cross-session memory: pip uv Terminalpip install "pydantic-ai-slim[openai]" from pydantic_ai import Agent from pydantic_ai.capabilities import LocalWorkspace, WebSearch from pydantic_ai_harness import Coder, Memory from pydantic_ai_harness.memory import FileMemoryStore agent = Agent( 'openai:gpt-6.1-sol', capabilities=[ LocalWorkspace('.'), Coder(), WebSearch(), # look up docs and error messages on the web Memory(FileMemoryStore('.agent-memory')), # remembers across sessions ], ) Skills (your SKILL.md procedures, loaded on demand; point it at a skills/ directory and add the skills extra), Web Fetch, Guardrails, and Dynamic Workflow slot in the same way. Built from capabilities Coder is a regular combined capability: FileSystem with five of its tools and content hashes off, Shell with its persistent shell tool and no allowlist, RepoContext, SubAgents delegating to the agent itself, ClearToolResults, and a bounded ToolOutputLimits, plus its default instructions and JSON argument repair. Use it whole, or build the same agent from those capabilities to change any setting; the Coder page lists the exact configuration, tool signatures, delegation, and the persistent shell lifecycle. On a remote sandbox, Coder names the working directory and loads repo instructions at run start, so it creates the sandbox before the model’s first request. Compose your own when Coder’s own settings cannot give you the agent you want. A code reader that greps and reads but never writes or runs anything nearly works with Coder: a read-only workspace (LocalWorkspace('.', read_only=True)) refuses every write and every command, and narrows the file tools to the read-only set. What no Coder setting does is take the shell away. It always adds Shell’s shell tool, so the model is offered a shell, calls it, and gets the workspace’s read-only refusal back as a failed tool result, instead of never having had the tool. Composing the same agent from capabilities leaves the tool out of the toolset: from pydantic_ai import Agent from pydantic_ai.capabilities import LocalWorkspace from pydantic_ai_harness import FileSystem, RepoContext from pydantic_ai_harness.compaction import ClearToolResults from pydantic_ai_harness.tool_output_limits import ToolOutputLimits agent = Agent( 'openai:gpt-6.1-sol', capabilities=[ LocalWorkspace('.'), FileSystem(tools=['read_file', 'list_files', 'grep']), # no write or edit tool RepoContext(), # your AGENTS.md and CLAUDE.md ClearToolResults(max_fraction=0.8, keep_pairs=3), # keeps long runs inside the window ToolOutputLimits(), # one large file cannot flood the context ], instructions='Answer from the code. Quote the file and line you got each claim from.', ) There is no Shell and no write tool in that list, so the agent has no way to run a command or change a file: the capabilities an agent does not have are the clearest statement of what it cannot do. The run itself still writes: ToolOutputLimits spills an oversized result to .pydantic-ai-harness/ in the workspace’s working directory. That is the whole pattern: pick the capabilities you want, configure them, and leave out the ones you do not. Each one is documented on its own page. Containing what an agent may do Leaving a capability out bounds an agent, but it is not containment: an agent that has Shell can run anything the user running it can. allowed_commands and allowed_patterns are guardrails against accidents, not a security boundary. The allowlist matches only a command’s first token, and an allowlisted git or python can spawn whatever it likes; see the warning on Shell. The workspace is the layer that bounds where commands run, and how far it bounds them depends on which one you pick. Wrapping one puts Coder unchanged inside it: from pydantic_ai import Agent from pydantic_ai.capabilities import LocalWorkspace from pydantic_ai_harness import BubblewrappedWorkspace, Coder agent = Agent( 'anthropic:claude-opus-5-5', capabilities=[ BubblewrappedWorkspace(LocalWorkspace('/srv/app')), Coder(), ], ) That keeps a trusted agent from accidentally writing outside its working directory: the file tools and commands can write only there, the rest of the host is mounted read-only, /tmp is private, and there is no network unless you pass network=True. It is not a security boundary, and not a sandbox for untrusted code or an untrusted agent, which is how BubblewrappedWorkspace puts it itself. Commands share the host kernel and can read every file the host user can, SSH keys and cloud credentials included, and show them to the model; they can signal the user’s other processes, and background jobs outlive the run. An agent you do not trust belongs on a machine that is not yours: E2B, Modal and Smol run commands in an isolated cloud sandbox, and the workspace list has the rest. Workspaces A workspace is where the agent’s files and commands live: your machine with LocalWorkspace, or an isolated sandbox. Harness capabilities never pick one for you. Attach one, or the run fails at its start and tells you what to attach. Coder, FileSystem, Shell, Repo Context, and Macroscope work in the run’s workspace, starting in its working directory. To work in a subdirectory, set it on the workspace: LocalWorkspace('./repo'). To continue in the same files from a later run or another agent, see Sharing a work