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

Coder | Pydantic Docs

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

最近抓取:2026-12-03 23:28:36(北京时间)

最近内容变化:2026-12-03 23:28:36(北京时间)

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

访问原网页

页面简介

Build a coding agent with Pydantic AI Harness: Coder gives any model read, write, edit, grep, and shell tools, delegation to a fresh run of itself, plus repo instructions and context management.

正文摘录

Coder Coder gives a Pydantic AI agent tools and guidance for investigating, editing, and testing a codebase. It works in the run’s workspace, on this machine or in a sandbox. It is a regular combined capability made from FileSystem, Shell, RepoContext, SubAgents, and the context management capabilities, so you can use it whole or take it apart. While Pydantic AI Harness is on 0.x releases, the API may change between minor releases; when it does, deprecation warnings and release-note migration guidance tell you (or your agent) exactly how to upgrade. See the version policy. flowchart LR accTitle: Coder: a coding agent assembled from harness capabilities accDescr: The model calls the Coder capability and gets results. Coder bundles FileSystem with read_file, write_file, edit_file, list_files and grep, Shell with shell, SubAgents with delegate_task, and RepoContext with AGENTS.md and CLAUDE.md. Coder works in the workspace, which is LocalWorkspace('.') or a cloud sandbox. model(["model"]) subgraph coder["Coder()"] direction LR fs["FileSystem<br/>read_file, write_file,<br/>edit_file, list_files, grep"]:::capability shell["Shell<br/>shell"]:::capability sub["SubAgents<br/>delegate_task"]:::capability repo["RepoContext<br/>AGENTS.md,<br/>CLAUDE.md"]:::capability end workspace["workspace<br/>LocalWorkspace('.')<br/>or a cloud sandbox"]:::external model <--> coder coder --> workspace classDef capability stroke:#E520E9,stroke-width:2px classDef external stroke:#0E9C86,stroke-width:2px Usage Install the Coder extra. It includes json-repair, which RepairToolArguments needs, ripgrep (rg), which backs the list_files and grep tools, and Pillow, which downscales large images read_file returns. Without the extra, importing Coder raises an ImportError naming it, and without Pillow, constructing Coder does, unless you pass max_image_side=None: pip uv Terminalpip install "pydantic-ai-harness[coder]" The extra installs ripgrep==14.1.0 except on Android, where rg must be supplied separately on PATH. Add a provider extra such as [coder,anthropic] when needed. Coder works in the run’s workspace. Here that is the current directory on your machine: 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) File paths resolve from the workspace’s working directory, and commands start there. To work in an isolated cloud machine instead, swap LocalWorkspace for a sandbox capability (Modal, E2B, Render, Sprites, Daytona, or boxd); the rest of the code stays the same. Commands run without an allowlist, and the file tools’ path limits don’t apply to them. With Modal, for example: from pydantic_ai_harness.modal_sandbox import ModalSandbox agent = Agent('anthropic:claude-opus-5-5', capabilities=[ModalSandbox(), Coder()]) Run Pydantic AI coding agents in E2B sandboxes runs a Coder agent in an E2B sandbox. With LocalWorkspace, agent.to_cli_sync() and agent.to_web() work in the same directory. With a sandbox, the CLI keeps one sandbox for the session, but to_web() starts a new one for each message because the web protocol does not carry the workspace ref, so use LocalWorkspace when files must persist between web messages. The exported pydantic_ai_harness.coder:coder_agent is the same agent, model-less and named coder, working in the directory that is current when it is imported. Pax runs this agent in your terminal, with --agent taking the import path. It loads no plugins, and needs a model because the export carries none: Terminaluvx pydantic-ai-pax --agent pydantic_ai_harness.coder:coder_agent -m anthropic:claude-opus-5-5 For a coding agent rather than this export, plain uvx pydantic-ai-pax builds its own Coder with Pax’s plugins. Search in a sandbox The Coder extra installs rg on the agent host, not in a sandbox image, so install rg and git in the image for fast search. Prefer the file tools (grep, list_files, read_file) to sending whole files through shell cat. In a workspace without rg, grep and list_files fall back to git, grep, and find where the workspace can run commands, and to a bounded file walk on filesystem-only workspaces. The fallback applies .gitignore only when git is available, and doesn’t support rg-specific regex features. Searches report when output was capped rather than presenting partial results as complete. The command environment Commands in a LocalWorkspace get your PATH, HOME, LANG, LC_ALL and LC_CTYPE, so they find your tools and their configuration and use your locale, and nothing else from your environment. python, pytest and other tools resolve through that PATH, so when you start the agent with uv run they come from the agent project’s virtualenv, not necessarily the workspace’s; pass LocalWorkspace('.', env={'PATH': ...}) to point commands at the workspace’s own interpreter. Pass only what they need with env=: import os 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('.', env={'GH_TOKEN': os.environ['GH_TOKEN']}), Coder()], ) Don’t pass os.environ: that hands the model’s commands every secret in the process, LLM API keys included. Sharing a workspace A workspace outlives the run that used it. To continue the conversation in the same files, pass its messages: 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('Add a --verbose flag to the CLI.') result = agent.run_sync('Document the new flag in the README.', message_history=result.all_messages()) Message history can’t move a LocalWorkspace to another directory. When you keep result.workspace after a run on a sandbox backend that has aclose(), call await result.workspace.backend.aclose() when you’re done with it to release its client session. This closes the client, not necessarily the sandbox; use the provider’s deletion API for sandboxes you own. To hand the work to another agent, or start a fresh conversation in the same files, pass the workspace itself: from pydantic_ai import Agent from pydantic_ai.capabilities import LocalWorkspace from pydantic_ai.workspaces import ReadOnlyWorkspace from pydantic_ai_harness.coder import Coder coder = Agent('anthropic:claude-opus-5-5', capabilities=[LocalWorkspace('.'), Coder()]) reviewer = Agent('anthropic:claude-opus-5-5', capabilities=[Coder(instructions='Review the code for bugs.')]) result = coder.run_sync('Add a --verbose flag to the CLI.') review = reviewer.run_sync('Review the new --verbose flag.', workspace=ReadOnlyWorkspace(result.workspace)) The reviewer works in the workspace you pass. ReadOnlyWorkspace refuses commands and file changes, so the reviewer gets read_file, list_files, grep, and delegate_task but no shell or editing tools; pass result.workspace itself to let it run commands and edit files. With a sandbox, this is how several agents share one isolated machine. SubAgents needs nothing extra: each delegate runs in the parent’s workspace. Composition Coder() is these capabilities, in this order: A Capability carrying the default instructions, plus any instructions= you pass. FileSystem(content_hashes=False, max_read_chars=50000, tools=FILE_TOOL_NAMES, max_retries=5, max_image_side=1024), where FILE_TOOL_NAMES is read_file, write_file, edit_file, list_files, and grep. Its root_dir is the workspace’s working directory. Each file tool allows five consecutive retries (for a denied path or a stale edit) rather than the agent’s default one, so a repeated correctable mistake does not end a long run. Images over 1024 pixels on the longest side are downscaled; Coder(max_image_side=None) sends them at full size and drops the Pillow requirement. Shell(denied_commands=[], allow_interactive=True, default_timeout=270, tools=['shell']). RepoContext(walk_up='repo_root') for repository instructions: the instruction file of each directory (AGENTS.md, else CLAUDE.md) from the repository root down to the working directory at run start, and those in subdirectories when the agent first reads, lists, writes or edits something under them. A subdirectory file that would take the conversation past RepoContext’s default budget of 32,000 characters of inlined instructions is replaced by a pointer to the file. Pass your own RepoContext as repo_context= to use it in this one’s place, or repo_context=False to leave it out when the agent already binds its own RepoContext, so the instruction files are not loaded twice. SubAgents(include_self=True, agent_folders=None), so the agent can hand a self-contained sub-task to a fresh run of itself (see below). Pass sub_agents=False to leave it out. Pass agent_folders= (same values as SubAgents) to also load disk-defined agents; it defaults to None. Then capabilities that shape the run without adding tools: ClearToolResults(max_fraction=0.7). A private ToolOutputLimits specialization that truncates any tool result over 64,000 characters without adding a spill-retrieval tool. Its stable ID, coder_tool_output_limits, lets durability capabilities bind its inherited operations without colliding with a separately configured ToolOutputLimits. RepairToolArguments repairs malformed JSON tool arguments before normal validation (see below). Once a tool’s retries run out while it executes, its last error goes back to the model as a failed tool result with a prompt to try a different approach, rather than ending the run. This covers every tool, delegate_task included, so consecutive delegate failures do not abort the parent. Running out on argument validation (see item 8) still ends the run, and usage limits