一个 Capability 捆绑 Agent 的指令、工具、生命周期钩子和模型设置,使整个扩展(如记忆系统、护栏、编码工具包)能够通过一个概念触及 Agent 的每一层。
from pydantic_ai import Agent
from pydantic_ai.capabilities import Capability, Thinking, ToolSearch, WebSearch
agent = Agent(
'anthropic:claude-opus-4-7',
instructions='Research thoroughly and cite your sources.',
capabilities=[
Thinking(effort='high'),
WebSearch(),
ToolSearch(),
Capability(
id='github',
description='Look up GitHub issues, pull requests, and code.',
instructions='Use the GitHub tools when a question is about a repository.',
toolset=MCPToolset('https://mcp.example.com/github'),
defer_loading=True,
),
],
)标记 defer_loading=True 的 Capability 在模型需要之前不会出现在提示中。模型只在紧凑目录中看到一行描述,然后在决定使用时才一次性加载整个捆绑包(包括指令和工具)。
Capabilities 通过钩子读取和重写模型在每一步看到的内容,包括其工具、指令和消息历史。代码模式和工具搜索都基于相同的公共钩子构建。