Anthropic Claude

agent-framework-claude 將 Claude Agent SDK 包裝為 ClaudeAgent。 它使用 Claude 的管理代理執行時、會話、權限模型、內建工具及 MCP 支援,同時開放 Agent Framework 執行與串流介面。

此整合有別於 Anthropic 模型提供者;後者是以 Claude 做為應用程式擁有的 Agent Framework 代理程式背後所使用的模型。

Prerequisites

  • 安裝並設定 Claude Code CLI。
  • 選擇 Claude 模型和權限模式。
  • 啟用檔案或 shell 工具時,請在受限的工作目錄中執行代理程式。

安裝套件

pip install agent-framework-claude --pre

Configuration

Variable Purpose
CLAUDE_AGENT_MODEL 受管執行階段所使用的 Claude 模型
CLAUDE_AGENT_PERMISSION_MODE 內建與 MCP 工具的預設權限模式。
CLAUDE_AGENT_CLI_PATH 選用的明確路徑,指向 Claude Code CLI。
CLAUDE_AGENT_CWD 供執行階段存取的工作目錄。
CLAUDE_AGENT_MAX_TURNS 選用的代理程式最大回合數。
CLAUDE_AGENT_MAX_BUDGET_USD 選用的執行成本預算。

建立 ClaudeAgent

ClaudeAgent 支援定期與串流執行,並可公開代理框架功能工具。

async def non_streaming_example() -> None:
    """Example of non-streaming response."""
    print("=== Non-streaming Example ===")

    agent = ClaudeAgent(
        name="BasicAgent",
        instructions="You are a helpful assistant. Keep responses concise.",
        tools=[get_weather],
    )

    async with agent:
        query = "What's the weather in Seattle?"
        print(f"User: {query}")
        result = await agent.run(query)
        print(f"Agent: {result.text}\n")


async def streaming_example() -> None:
    """Example of streaming response."""
    print("=== Streaming Example ===")

    agent = ClaudeAgent(
        name="StreamingAgent",
        instructions="You are a helpful assistant.",
        tools=[get_weather],
    )

    async with agent:
        query = "What's the weather in Paris?"
        print(f"User: {query}")
        print("Agent: ", end="", flush=True)
        async for chunk in agent.run(query, stream=True):
            if chunk.text:
                print(chunk.text, end="", flush=True)
        print("\n")

其他範例顯示:

  • Claude 內建檔案與 Shell 工具。
  • 互動式權限處理。
  • 本地與遠端 MCP 伺服器。
  • 會話持續與恢復。
  • 連續式工作流程,會在 Claude 代理間傳遞先前訊息。
  • URL 取用與多重權限規則。

當收到多則訊息時 ClaudeAgent ,會發送一個提示,將訊息框定為對話歷史,並依角色標示每則訊息。 這種方式在代理切換時保留角色邊界,但不會重播原生 Claude 多角色的歷史,也不會共享 Claude 會話。 單一使用者訊息保持不變。

許可考量

  • 從支援該任務的最低許可 Claude Agent SDK 權限模式開始。
  • 要求對 shell、檔案、網路或其他副作用操作進行明確核准。
  • 不要透過環境變數或代理工作目錄中可讀的檔案來暴露憑證。

下一步