页面简介
Load Agent Skills (SKILL.md files) from the run's workspace into a Pydantic AI agent on demand: the model sees each skill's name and description and loads its instructions when needed.
正文摘录
Skills Use Agent Skills to give an agent specialized instructions without putting every instruction in its initial prompt. Point Skills at one or more skill libraries. The model first sees each skill’s name and description, followed by its when_to_use text if it has one. When a skill is useful, the model loads it with the load_capability tool to receive that skill’s instructions. Source 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 TB accTitle: Skills: load SKILL.md instructions on demand accDescr: At run start, the Skills capability reads the workspace files under .agents/skills/, here code-review/SKILL.md and release-notes/SKILL.md. It puts each skill's name and description in the model's instructions. When a skill is needed, the model calls load_capability and gets a heading, the skill directory and the SKILL.md body. subgraph workspace["Workspace"] files[".agents/skills/<br/>code-review/SKILL.md<br/>release-notes/SKILL.md"]:::external end skills["Skills()"]:::capability model(["model"]) load["load_capability"]:::capability files -->|"read at run start"| skills skills -->|"instructions: each skill's<br/>name and description"| model model <-->|"when a skill is needed:<br/>heading, skill directory,<br/>SKILL.md body"| load classDef capability stroke:#E520E9,stroke-width:2px classDef external stroke:#0E9C86,stroke-width:2px classDef blocked stroke:#E5533D,stroke-width:2px,stroke-dasharray:4 3 Installation Install the skills extra for YAML frontmatter support: pip uv Terminalpip install "pydantic-ai-harness[skills]" Quick start Create a skill library: .agents/skills/ code-review/ SKILL.md Add the skill’s description and instructions: --- name: code-review description: Review a change for correctness and repository conventions. --- Inspect the change and report findings by severity. Then add the library to your agent, with a workspace to read it from: from pydantic_ai import Agent from pydantic_ai.capabilities import LocalWorkspace from pydantic_ai_harness import Skills agent = Agent( 'anthropic:claude-opus-5-5', capabilities=[LocalWorkspace('.'), Skills('.agents/skills')], ) Skills does not search .agents, .claude, or your home directory automatically. Pass each library you want it to load; to layer several the way coding agents do, see Layer project and personal skills. How it works At the start of every run, Skills: Reads each configured library from the run’s workspace. Relative paths resolve from its working directory. Scans the immediate child directories and validates the selected SKILL.md files. Offers each selected skill as a deferred capability named after it: the model sees its name, description and when_to_use, and loads it with the load_capability tool. Loading a skill returns a # Skill: <name> heading, a line naming the skill’s directory in the workspace, and the skill’s Markdown body. The catalog is the same on every run over the same files, so it stays in the cached prefix; a new or renamed skill appears in it on the next run. Without any skills, Skills adds no instructions and no load_capability tool. A run without a workspace fails at its start, unless every library has its own workspace= backend, as in Ship skills with your application. Ship skills with your application To load skills that ship inside your application, keep Skills and pass workspace= a LocalWorkspaceBackend rooted at your application’s files. Skills then reads its libraries from that directory instead of the run’s workspace, and discovers, validates, and selects SKILL.md packages the same way. The agent needs no workspace of its own: from pathlib import Path from pydantic_ai import Agent from pydantic_ai.workspaces import LocalWorkspaceBackend from pydantic_ai_harness import Skills APP_DIR = Path(__file__).parent # contains skills/code-review/SKILL.md agent = Agent( 'anthropic:claude-opus-5-5', capabilities=[Skills('skills', workspace=LocalWorkspaceBackend(APP_DIR))], ) Relative library paths resolve against the backend’s directory, so 'skills' above is APP_DIR / 'skills'. LocalWorkspaceBackend takes a path on disk: build it from Path(__file__), not from importlib.resources.files(), which returns a Traversable that is a real directory only when the package is installed unzipped. The model sees each skill’s name and description in the catalog, and loading a skill returns its heading and SKILL.md body. The loaded instructions do not name the skill’s directory, since the model’s file and shell tools don’t work there. Pass read_files=True to let the model read a loaded skill’s references/ and other text files from APP_DIR. To run its scripts/, put its library in the run’s workspace instead and give the agent Shell there; see Bundled files. The same backend works when the agent’s own workspace is a sandbox: Skills reads the skills from your application’s files, and the agent’s tools act in the sandbox. workspace= takes a backend, not the LocalWorkspace capability. Under a durable engine such as Temporal, a workspace= backend is read in-process, not through the workflow. For instructions that are not SKILL.md packages in a directory, such as runbooks kept in a database or files in a custom format, skip Skills and define each one as a deferred Capability: Loading skills from Markdown files shows a file loader, and Loading runbooks from a database reads the instructions at run time. Skills from files you didn’t write The model reads the catalog as your instructions, so a skill whose SKILL.md is below 'operator' authority, such as one in a repository the operator doesn’t control, stays out of it. Instead, the instructions point the model at two tools whose results reach it as tool output rather than as your instructions: list_skills returns those skills’ names and descriptions, with any when_to_use text. load_skill returns one skill’s instructions. With read_files=True, read_skill_file also reads the files of a skill list_skills lists, without loading it first. Each of these costs a tool call when a task may need a skill. The tools offer the skills the run started with, read again on each call. A skill skipped on that read, such as one an artifact scan now flags, is reported in the tool’s result; the run warns about skips only once, at its start. Each skill takes the level of its own file, Workspace.authority_of, in the workspace its library is read from. So with a run workspace at authority='none', Skills('skills', workspace=LocalWorkspaceBackend('/app')) keeps its skills in the catalog, while Skills('skills'), read from the run’s workspace, offers its skills through list_skills; and a path_authority entry such as {'skills/vendored': 'none'} moves just those skills. Choose which skills to expose By default, all discovered skills are included. Use include or exclude to change the catalog for a particular agent: from pydantic_ai_harness import Skills review_skills = Skills( '.agents/skills', include=['code-review'], ) release_skills = Skills( '.agents/skills', exclude=['code-review'], ) ConfigurationSkills in the catalogNeither optionAll discovered skillsinclude=['a', 'b']Only a and binclude=[]No skillsexclude=['a', 'b']All except a and bexclude=[]All discovered skills include and exclude cannot be used together. The constructor overloads catch this in typed code, and runtime validation covers agent specs and untyped callers. Unknown names fail at run start. Pass unknown_names='skip' to ignore them instead, for a selection shared by projects that do not all have every skill it names, such as a person’s list of skills to hide. Selection happens before frontmatter is parsed. An unselected skill does not add instructions or frontmatter validation errors to that Skills instance. These options control catalog exposure. They are not filesystem permissions or an access-control boundary. Directory paths choose where discovery starts in the workspace; they do not create a containment boundary, and normal symlink resolution applies. A selected SKILL.md body becomes model instructions. Load libraries only from sources you trust, and review repository-provided skills before exposing them. Skill format Each immediate child directory containing SKILL.md is a skill: .agents/skills/ code-review/ SKILL.md release-notes/ SKILL.md The loader uses these parts of SKILL.md: PartRequirementnameOptional. Defaults to the parent directory name. If provided, it must match the directory after Unicode normalization.descriptionRequired and non-blank. The Agent Skills limit is 1,024 characters; longer descriptions load with a warning. This appears in the initial catalog.when_to_useOptional. Appended to the description in the catalog, so write the whole trigger clause, such as “Use when …”. A blank value counts as unset.Markdown bodyOptional. This is loaded under a generated # Skill: <name> heading.disable-model-invocationOptional, true or false. true leaves the skill out of the model’s catalog; see Invoke a skill yourself.user-invocableOptional, true or false. false tells a host not to offer the skill as a command; the model still sees it. Skill names and include or exclude values are normalized with Unicode NFKC before matching. A normalized name can contain at most 64 lowercase Unicode letters or numbers, separated by single hyphens. It cannot start or end with a hyphen. disable-model-invocation and user-invocable come from Claude Code. Any value other than true or false (in any letter case) skips the skill with a warning, rather than guessing whether it may be invoked. Only immediate children are discovered. For example, code-review/references/SKILL.md does not create another skill. Ordinary files and child directories without SKILL.md are ignored. You can pass several libraries: from pydantic_ai_harness import Skills skills = Skills([ '.agents/skills', 'company/skills'