harness-sdk

框架

strands-agents/harness-sdk

开源 Python 与 TypeScript 的 AI 智能体开发 SDK。

项目简介

Strands Agents 是一个模型驱动的开源 SDK,用于在 Python 与 TypeScript 中构建和运行 AI 智能体。它内置生命周期控制、工具调用、结构化输出、MCP、多智能体模式、记忆、会话、流式输出、护栏、追踪与评测,并支持 Bedrock、Anthropic、OpenAI、Gemini 等多种模型提供商。

README 预览

\n  \n    \n      \n        \n        \n      \n    \n  \n\n  \n    Strands Agents\n  \n\n  \n    A model-driven approach to building AI agents in just a few lines of code.\n  \n\n  \n    \n    \n    \n    \n    \n    \n    \n    \n    \n  \n  \n  \n    Documentation\n    ◆ Samples\n    ◆ MCP Server\n    ◆ Discord\n  \n\n\nStrands Agents is an open-source SDK for building and running AI agents in Python and TypeScript. Choose Strands when you would otherwise write your own agent loop: it runs in your process with no hosted control plane, and it covers the jobs a hand-rolled loop grows into. In one SDK you get [lifecycle controls](https://strandsagents.com/docs/user-guide/concepts/agents/agent-loop/) (turn limits, token budgets, cancellation, stop reasons), [tools](https://strandsagents.com/docs/user-guide/concepts/tools/) and [structured output](https://strandsagents.com/docs/user-guide/concepts/agents/structured-output/), [MCP](https://strandsagents.com/docs/user-guide/concepts/tools/mcp-tools/), [multi-agent patterns](https://strandsagents.com/docs/user-guide/concepts/multi-agent/multi-agent-patterns/), [memory](https://strandsagents.com/docs/user-guide/concepts/memory/overview/) and [sessions](https://strandsagents.com/docs/user-guide/concepts/agents/session-management/), [model portability](https://strandsagents.com/docs/user-guide/concepts/model-providers/), [streaming](https://strandsagents.com/docs/user-guide/concepts/streaming/), [guardrails](https://strandsagents.com/docs/user-guide/safety-security/guardrails/), [tracing](https://strandsagents.com/docs/user-guide/observability-evaluation/observability/), and [evals](https://strandsagents.com/docs/user-guide/evals-sdk/quickstart/).\n\nThis monorepo contains Strands harness, the Python and TypeScript SDKs, the documentation site, and supporting packages:\n\n| Directory | Description |\n|-----------|-------------|\n| [`harness-py/`](harness-py/) | Python Strands harness: fully assembled agent via `create_harne

常见问题 (5)

故障排除
如何修复在另一个智能体中将 Strands Agent 用作工具时出现的输出交错或重复问题?

在子代理上设置 callback_handler=None,这样它的 token 流就不会通过编排器的输出通道发出。子代理仍会运行并返回结果,但只有父代理/编排器会流式输出最终响应。

sub_agent = Agent(
    model=model,
    system_prompt="You are a news fetcher.",
    tools=[some_tool],
    callback_handler=None  # Required when this agent is used as a tool
)

@tool(name="live_news", description="Fetches latest news")
def live_news(input: str) -> str:
    result = sub_agent(input)
    return str(result)  # Clean text returned to the orchestrator

orchestrator = Agent(
    model=model,
    system_prompt="You are Jarvis.",
    tools=[live_news]
)

不要在编排器上禁用回调处理器,除非你也想抑制它的最终输出。在 TypeScript 自定义代理包装器中,等效的设置是在嵌套代理上设置 printer: false。

来源 Issue #2605
故障排除
如何修复 Strands MCPClient 出现的 'Unsupported protocol version from the server: 2026-07-28' 错误?

将 strands-agents 升级到 1.55.0 或更高版本:pip install -U strands-agents。旧版本将依赖固定为 mcp>=1.23.0,<2.0.0,并使用旧版的 ClientSession.initialize() 握手方式,该方式会拒绝协商现代 2026-07-28 协议的服务器(例如使用 supportedVersions=["2026-07-28"] 的 AWS Bedrock AgentCore Gateway)。在 v1.55.0 中,依赖范围允许 mcp 2.x,并且 MCPClient 通过 negotiate_session 建立连接,它会在 mcp 2.x 上协商较新的协议,同时对旧版本服务器透明地回退到旧版握手方式。

来源 Issue #4038
故障排除
为什么 Strands OpenAIModel 在聊天(chat)模式下通过 OpenRouter 报告的 token 用量为零?

OpenRouter 会在同时包含 choices[0].finish_reason 的最后一个流式数据块中发送 usage 对象。在 Strands TypeScript SDK 1.19.0 中,_streamChat 只从 choices 数组为空的数据块中读取 usage,因此最后一个数据块会被跳过,agent.metrics.accumulatedUsage 保持为 { inputTokens: 0, outputTokens: 0, totalTokens: 0 }。

临时解决方案:改用 Responses API:

new OpenAIModel({
  api: 'responses',
  modelId: 'openai/gpt-6-luna',
  apiKey: process.env.OPENROUTER_API_KEY,
  clientConfig: { baseURL: 'https://openrouter.ai/api/v1' },
})

代码修复:在 _streamChat 中,在检查 choices 是否为空之前,先从每个数据块读取 chunk.usage:

if (chunk.usage) {
  bufferedUsage = chunk.usage
}

if (!chunk.choices || chunk.choices.length === 0) {
  continue
}

如果可用,请升级到包含修复 fix(openai): read chat usage reported on the finish_reason chunk 的 Strands 版本。

来源 Issue #4847
故障排除
为什么 Strands 为 Claude Haiku 5.5 使用 200k 上下文窗口,而不是 1M?

Strands 1.58.1 没有针对 Claude Haiku 5.5 的上下文窗口映射,因此 BedrockModel.context_window_limit 会回退到默认的 200,000 个 token。这会使 estimate_utilization(150000) 返回 0.75 而不是 0.15,并且可能在 0.7 阈值下于约 140k token 时(而非 700k)触发主动摘要。

临时解决方法:显式设置该限制。

from strands.models import BedrockModel

model = BedrockModel(
    model_id="eu.anthropic.claude-haiku-5-5",
    region_name="eu-west-2",
    context_window_limit=1_000_000,
)

对 global.anthropic.claude-haiku-5-5 使用相同的值。永久修复方法是升级到包含 PR #4998 的 Strands 版本,该 PR 为 Python 和 TypeScript 的默认配置都添加了 anthropic.claude-haiku-5-5 和 claude-haiku-5-5 的 1,000,000 token 条目。

来源 Issue #4996
故障排除
为什么在未设置 AWS_REGION 时,Strands CLI 的 Bedrock 模型列表为空?

此问题发生在 @strands-agents/cli 0.1.0 中,因为 STS 凭证验证会回退到 us-east-1,而 Bedrock 模型发现在未解析到区域时会省略该区域,导致目录请求静默失败并返回零个模型。

临时解决方法:在启动 CLI 之前设置区域:

export AWS_REGION=us-east-1
strands

或者,将 config.json 中的 providers.environment.AWS_REGION 设置为您的 Bedrock 模型所在的区域。

该修复已通过 PR #4581(commit 9b7657f1)在 main 分支上提供。awsClientConfiguration 现在按照一致的优先级解析区域:显式指定的区域 → 配置文件派生的区域 → us-east-1。请更新到包含此修复的 CLI 版本。如果 config.json 被重写且 providers.environment.AWS_REGION 消失,则属于另一个尚未解决的问题,应单独报告。

来源 Issue #4481