AdSense

網頁

2026/6/15

LangChain 使用LangSmith監控Agent

使用LangSmith監控Agent的行為。


環境

Windows 11。


事前要求

LangChain 建立一個LangChain專案

Anthropic Claude Code取得API key

LangSmith 取得API Key

Python 安裝python-dotenv


建立.env

在專案根目錄建立.env,內容如下。

.env

LANGSMITH_TRACING=true
LANGSMITH_ENDPOINT=https://apac.api.smith.langchain.com
LANGSMITH_API_KEY=lsv2_pt_-----df84d744dc98be8f39ccd97fc86_756a02616b
LANGSMITH_PROJECT="langchain-demo"

ANTHROPIC_API_KEY=sk-a---------------------------------------------E1Kcuw771HCta9y00URWVulgElyzIgG6x2UKCpawWVoAi5ndg-uqjzfQAA


撰寫程式

main.py撰寫以下內容,此為LangSmith Tracing的quick start範例,但由於使用.env設定API Key,所以前面要先加上load_dotenv()讀取.env中的變數設定。

main.py

from dotenv import load_dotenv
load_dotenv()

import asyncio
from typing import Any

from claude_agent_sdk import (
    ClaudeAgentOptions,
    ClaudeSDKClient,
    create_sdk_mcp_server,
    tool,
)
from langsmith.integrations.claude_agent_sdk import configure_claude_agent_sdk

configure_claude_agent_sdk()


@tool(
    "get_weather",
    "Gets the current weather for a given city",
    {"city": str},
)
async def get_weather(args: dict[str, Any]) -> dict[str, Any]:
    city = args["city"]
    weather_data = {
        "San Francisco": "Foggy, 62°F",
        "New York": "Sunny, 75°F",
        "London": "Rainy, 55°F",
        "Tokyo": "Clear, 68°F",
    }
    weather = weather_data.get(city, "Weather data not available")
    return {"content": [{"type": "text", "text": f"Weather in {city}: {weather}"}]}


async def main() -> None:
    weather_server = create_sdk_mcp_server(
        name="weather",
        version="1.0.0",
        tools=[get_weather],
    )

    options = ClaudeAgentOptions(
        model="claude-sonnet-4-5-20250929",
        system_prompt="You are a friendly travel assistant who helps with weather information.",
        mcp_servers={"weather": weather_server},
        allowed_tools=["mcp__weather__get_weather"],
    )

    async with ClaudeSDKClient(options=options) as client:
        await client.query("What's the weather like in San Francisco and Tokyo?")

        async for message in client.receive_response():
            print(message)


if __name__ == "__main__":
    asyncio.run(main())

github


測試

輸入un run main.py執行程式,印出以下結果。

SystemMessage(
    subtype="init",
    data={
        "type": "system",
        "subtype": "init",
        "cwd": "C:\\cust\\learn\\langchain\\langchain-demo",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
        "tools": [
            "Task",
            "AskUserQuestion",
            "Bash",
            "CronCreate",
            "CronDelete",
            "CronList",
            "DesignSync",
            "Edit",
            "EnterPlanMode",
            "EnterWorktree",
            "ExitPlanMode",
            "ExitWorktree",
            "Glob",
            "Grep",
            "Monitor",
            "NotebookEdit",
            "PowerShell",
            "PushNotification",
            "Read",
            "ScheduleWakeup",
            "Skill",
            "TaskCreate",
            "TaskGet",
            "TaskList",
            "TaskOutput",
            "TaskStop",
            "TaskUpdate",
            "ToolSearch",
            "WebFetch",
            "WebSearch",
            "Workflow",
            "Write",
            "mcp__weather__get_weather",
        ],
        "mcp_servers": [{"name": "weather", "status": "connected"}],
        "model": "claude-sonnet-4-5-20250929",
        "permissionMode": "default",
        "slash_commands": [
            "deep-research",
            "design-sync",
            "update-config",
            "verify",
            "debug",
            "code-review",
            "simplify",
            "batch",
            "fewer-permission-prompts",
            "loop",
            "claude-api",
            "run",
            "run-skill-generator",
            "clear",
            "compact",
            "context",
            "heapdump",
            "init",
            "reload-skills",
            "review",
            "security-review",
            "usage",
            "insights",
            "goal",
            "team-onboarding",
        ],
        "apiKeySource": "ANTHROPIC_API_KEY",
        "claude_code_version": "2.1.178",
        "output_style": "default",
        "agents": ["claude", "Explore", "general-purpose", "Plan", "statusline-setup"],
        "skills": [
            "deep-research",
            "design-sync",
            "update-config",
            "verify",
            "debug",
            "code-review",
            "simplify",
            "batch",
            "fewer-permission-prompts",
            "loop",
            "claude-api",
            "run",
            "run-skill-generator",
        ],
        "plugins": [],
        "analytics_disabled": False,
        "product_feedback_disabled": False,
        "uuid": "5a44a0ab-00b7-4643-ad8a-f40a56db7bf7",
        "memory_paths": {
            "auto": "C:\\Users\\bronx\\.claude\\projects\\C--cust-learn-langchain-langchain-demo\\memory\\"
        },
        "fast_mode_state": "off",
    },
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 10,
        "estimated_tokens_delta": 10,
        "uuid": "3dfb8670-4e3f-48c6-8cdd-ed4dd866e9b4",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 34,
        "estimated_tokens_delta": 24,
        "uuid": "89d9fd2b-885a-4761-b4ab-d469467c71ef",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 43,
        "estimated_tokens_delta": 9,
        "uuid": "7439696d-8f06-4aeb-876e-6eec7ac2e0a3",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 54,
        "estimated_tokens_delta": 11,
        "uuid": "2c7e31c5-86f8-4c81-a967-8dbc95587613",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 70,
        "estimated_tokens_delta": 16,
        "uuid": "5d629414-c979-4208-b5ba-ddd6255b3e5c",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 90,
        "estimated_tokens_delta": 20,
        "uuid": "fa949d18-682e-432c-9bb8-9e051b74d02c",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 115,
        "estimated_tokens_delta": 25,
        "uuid": "0d0d8a31-3475-4be5-af12-725467fb02d5",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 168,
        "estimated_tokens_delta": 53,
        "uuid": "f52dbf24-dacd-43b0-b2fe-b6e0b8727156",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
AssistantMessage(
    content=[
        ThinkingBlock(
            thinking="The user is asking about the weather in San Francisco and Tokyo. I can see in the system reminder that there's a deferred tool called `mcp__weather__get_weather` available. Since I'm described as \"a friendly travel assistant who helps with weather information,\" this is exactly the kind of request I should handle.\n\nFirst, I need to use ToolSearch to load the schema for the weather tool, then I can call it to get the weather information for both cities.",
            signature="EpgFCm4IDhgCKkAChaQrV54YR+94Z/RkzrZiuIXwbdxymvyD6KFM6R1TRc1daUNuU9gsFRtyCx43zL8G0GlubEgy/xCaPWfP1qohMhpjbGF1ZGUtc29ubmV0LTQtNS0yMDI1MDkyOTgAQgh0aGlua2luZxIM49E9FZXxJ5lM+SETGgy7mJYKo/m5SN0PPfkiMNW6xjayKJlVbEbrzGK31A9GZuQewAhl9TGv9RuZNkhSIQuGRmAjyia/zCvJw2JOhirXA/SDe5845RoNdGbl95fdmOcAspsvqTj/xiMgVlSIEiaWgxLyKPfjGYCuHPFQXc1UyxKREThQMNXcnVeIkTw37TLOtix9onWKbw0ALW3u0NSwS6jTs9qndxjEEAjU7nMCDqvECoyWq2dGXACLpmRW2RmfehJyunz5K/cXQ5LKB716xkWoXlq1n4g1m6iQAOLWWKbdfhbrscoPBQwcnJiPAvLsgfQdiC6M+Nequ5v8rqYnDQJ9B8/dcJEX8TU4Ho7wkanIlP6pd5p/0CSdh3bUXYyHssC+BnD6AYIfbfEQRuYNg9BULvDrrWkEi1sxnrOBS3HxQ0Ip4I/IqY/hglmB2DaUZS85QMi2dmxgx557c3JstHtQ2wxJ5xeqDLJTMFD99OXun7fgX1nLZPOnb7r/KaGjPmkM6Xc8eGXbRN6DGte/Y/Ttvvhww0JwGqg/gmI3tDw2gEzVP8b2ILIMGjhVe5zXekRs4jYqoDw9p6NLb5xz7LEeh71Iciak3W4I0RP5SbXtBvnReZ8aj+GGhTTeWuYzyFqomRNTQOeWssPgQ9MMeSsEp31UC9kVtY1OQJhgGmlbczE0jLUQDU85bJdPN5AG43CZ1tCzZTGIagOONyJX7QbjgLt0xxgB",
        )
    ],
    model="claude-sonnet-4-5-20250929",
    parent_tool_use_id=None,
    error=None,
    usage={
        "input_tokens": 10,
        "cache_creation_input_tokens": 0,
        "cache_read_input_tokens": 21663,
        "cache_creation": {
            "ephemeral_5m_input_tokens": 0,
            "ephemeral_1h_input_tokens": 0,
        },
        "output_tokens": 8,
        "service_tier": "standard",
        "inference_geo": "not_available",
    },
    message_id="msg_01KNv227tc76HPrhiR5WrLGG",
    stop_reason=None,
    session_id="8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    uuid="42916730-24bf-40c9-9cdd-12b675627e11",
)
AssistantMessage(
    content=[
        TextBlock(
            text="I'd be happy to help you check the weather in San Francisco and Tokyo! Let me fetch that information for you."
        )
    ],
    model="claude-sonnet-4-5-20250929",
    parent_tool_use_id=None,
    error=None,
    usage={
        "input_tokens": 10,
        "cache_creation_input_tokens": 0,
        "cache_read_input_tokens": 21663,
        "cache_creation": {
            "ephemeral_5m_input_tokens": 0,
            "ephemeral_1h_input_tokens": 0,
        },
        "output_tokens": 8,
        "service_tier": "standard",
        "inference_geo": "not_available",
    },
    message_id="msg_01KNv227tc76HPrhiR5WrLGG",
    stop_reason=None,
    session_id="8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    uuid="b4ad2c03-b652-46da-a783-eaa812d14bc9",
)
AssistantMessage(
    content=[
        ToolUseBlock(
            id="toolu_016iXJcP3v5TjJh7vJmoYy8B",
            name="ToolSearch",
            input={"query": "select:mcp__weather__get_weather", "max_results": 1},
        )
    ],
    model="claude-sonnet-4-5-20250929",
    parent_tool_use_id=None,
    error=None,
    usage={
        "input_tokens": 10,
        "cache_creation_input_tokens": 0,
        "cache_read_input_tokens": 21663,
        "cache_creation": {
            "ephemeral_5m_input_tokens": 0,
            "ephemeral_1h_input_tokens": 0,
        },
        "output_tokens": 8,
        "service_tier": "standard",
        "inference_geo": "not_available",
    },
    message_id="msg_01KNv227tc76HPrhiR5WrLGG",
    stop_reason=None,
    session_id="8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    uuid="e19490a9-3afe-48dd-b3bb-f7f91f1b7b38",
)
UserMessage(
    content=[
        ToolResultBlock(
            tool_use_id="toolu_016iXJcP3v5TjJh7vJmoYy8B",
            content=[
                {"type": "tool_reference", "tool_name": "mcp__weather__get_weather"}
            ],
            is_error=None,
        )
    ],
    uuid="d3fd9629-9338-4c3a-8cd8-851df9121035",
    parent_tool_use_id=None,
    tool_use_result={
        "matches": ["mcp__weather__get_weather"],
        "query": "select:mcp__weather__get_weather",
        "total_deferred_tools": 20,
    },
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 2,
        "estimated_tokens_delta": 2,
        "uuid": "970132cf-3a91-4bd0-a44a-4671b3228e89",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 21,
        "estimated_tokens_delta": 19,
        "uuid": "31869b56-aa53-4294-87af-822c0791e92a",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 44,
        "estimated_tokens_delta": 23,
        "uuid": "fc6b327f-eddc-42b9-992e-590e8cb2ab91",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 63,
        "estimated_tokens_delta": 19,
        "uuid": "a6329116-1497-4fd7-8bc1-f511c1077c2e",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 116,
        "estimated_tokens_delta": 53,
        "uuid": "8401adc3-ba09-4996-b603-b8cce4abb612",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
AssistantMessage(
    content=[
        ThinkingBlock(
            thinking='Great! Now I have the schema for the weather tool. It takes a "city" parameter as a string. I need to call it twice - once for San Francisco and once for Tokyo. Since these are independent calls, I should make them in the same function_calls block.',
            signature="EskDCm4IDhgCKkAVg5fZ8yfsYGVjfRI7PHLbl5MRDxLa99zHb2eEt8LXxgl1BSOWAVX27fZDP0g+8qhteszYNxrf753GX3M1xv0IMhpjbGF1ZGUtc29ubmV0LTQtNS0yMDI1MDkyOTgAQgh0aGlua2luZxIMCGhl5prw1vtSvoVdGgyxyJh/m8KfVfhg5cciMIzjJoy8Eq0jC0f9ezWlNhy7YmHNdOhpQ5KTbPrHMo/BoMxi9G/n6nKPpf0Z+f3EKCqIAk4BR+BiO4rH4/cG0GgZYbE9K+MNXolZBUy9608c7h7fmq5PLJlOeE2vMozyBNHcmcGdXKR8fogpIBbfA7HI3kHr2SFk6tWiILu7a0Gd1UNxKFAZX85Fz+pvortQGd07AQNL7Xg+PWlGNeSI8qlG28PsGZFJ/f95l+Ju5iY42Xp8i7P0QkJHCx/0OODzLy8C/AcIS/hpVHsoymCScH8o6umSoRtPAxbOU+3h7jOgc8XskFfVKbp/6MO4vmgfIhO9EG1wzIRSSqf1yog13qAgRhPfshw2dq/EFQM9ZunzSxY12grqi6imb9xdB0SJiijJ1CuCnE/PUS75PQBVI7e48GGhsg1p1hdF+hgB",
        )
    ],
    model="claude-sonnet-4-5-20250929",
    parent_tool_use_id=None,
    error=None,
    usage={
        "input_tokens": 10,
        "cache_creation_input_tokens": 311,
        "cache_read_input_tokens": 21663,
        "cache_creation": {
            "ephemeral_5m_input_tokens": 311,
            "ephemeral_1h_input_tokens": 0,
        },
        "output_tokens": 1,
        "service_tier": "standard",
        "inference_geo": "not_available",
    },
    message_id="msg_014xA1irxg9hH3oWcK4Cusw5",
    stop_reason=None,
    session_id="8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    uuid="de68467e-5b93-4d33-a6ac-64297dff87d5",
)
AssistantMessage(
    content=[
        ToolUseBlock(
            id="toolu_0145YdhPW1kAzFwdXbHjSQC8",
            name="mcp__weather__get_weather",
            input={"city": "San Francisco"},
        )
    ],
    model="claude-sonnet-4-5-20250929",
    parent_tool_use_id=None,
    error=None,
    usage={
        "input_tokens": 10,
        "cache_creation_input_tokens": 311,
        "cache_read_input_tokens": 21663,
        "cache_creation": {
            "ephemeral_5m_input_tokens": 311,
            "ephemeral_1h_input_tokens": 0,
        },
        "output_tokens": 1,
        "service_tier": "standard",
        "inference_geo": "not_available",
    },
    message_id="msg_014xA1irxg9hH3oWcK4Cusw5",
    stop_reason=None,
    session_id="8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    uuid="ab788de5-ee2e-4309-9969-8df796dc81a0",
)
UserMessage(
    content=[
        ToolResultBlock(
            tool_use_id="toolu_0145YdhPW1kAzFwdXbHjSQC8",
            content=[{"type": "text", "text": "Weather in San Francisco: Foggy, 62°F"}],
            is_error=None,
        )
    ],
    uuid="c3729dd0-3c5b-41da-8d89-f81acf613d10",
    parent_tool_use_id=None,
    tool_use_result=[{"type": "text", "text": "Weather in San Francisco: Foggy, 62°F"}],
)
AssistantMessage(
    content=[
        ToolUseBlock(
            id="toolu_01VwssQcQqjR7jmQYniBxxbF",
            name="mcp__weather__get_weather",
            input={"city": "Tokyo"},
        )
    ],
    model="claude-sonnet-4-5-20250929",
    parent_tool_use_id=None,
    error=None,
    usage={
        "input_tokens": 10,
        "cache_creation_input_tokens": 311,
        "cache_read_input_tokens": 21663,
        "cache_creation": {
            "ephemeral_5m_input_tokens": 311,
            "ephemeral_1h_input_tokens": 0,
        },
        "output_tokens": 1,
        "service_tier": "standard",
        "inference_geo": "not_available",
    },
    message_id="msg_014xA1irxg9hH3oWcK4Cusw5",
    stop_reason=None,
    session_id="8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    uuid="0eae5443-783f-48e6-9071-08e3e0535be6",
)
UserMessage(
    content=[
        ToolResultBlock(
            tool_use_id="toolu_01VwssQcQqjR7jmQYniBxxbF",
            content=[{"type": "text", "text": "Weather in Tokyo: Clear, 68°F"}],
            is_error=None,
        )
    ],
    uuid="892d2084-071d-477c-bd66-b0a863853a9b",
    parent_tool_use_id=None,
    tool_use_result=[{"type": "text", "text": "Weather in Tokyo: Clear, 68°F"}],
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 4,
        "estimated_tokens_delta": 4,
        "uuid": "276a0cd0-0f6b-448f-8203-b6b5c540c29c",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 32,
        "estimated_tokens_delta": 28,
        "uuid": "c109d196-389e-40d6-8d53-2f8db3649695",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
SystemMessage(
    subtype="thinking_tokens",
    data={
        "type": "system",
        "subtype": "thinking_tokens",
        "estimated_tokens": 85,
        "estimated_tokens_delta": 53,
        "uuid": "9670a657-7b67-4db0-88cd-32d68ba444f2",
        "session_id": "8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    },
)
AssistantMessage(
    content=[
        ThinkingBlock(
            thinking="Perfect! I got the weather information for both cities. Now I should present this information in a friendly way to the user.",
            signature="Es0CCm4IDhgCKkBu3Vem8UeuobATQLCP7cNy038QC3jfJw+y1anzOEJYFBEZA2Q/mJ4H6Ybdpvr6o/Gm7FA9h8T9e/9tNK9UL6MRMhpjbGF1ZGUtc29ubmV0LTQtNS0yMDI1MDkyOTgAQgh0aGlua2luZxIMZ8/YpOourghxpceKGgwAKWVeX0U2r/hIdQ8iMAmA5tQcXyFzpTh0RgzGJqk0UdlRU/PGNWi121UvRNCIlhIo/1iywYafbWYT5AxLUyqMAcob1adjdhF0HBB5InaDzSyzDStjGCQ6+ML9CnaPBboaBGK+YikSll8omX/wzHvBXVfalAerDVla1KWiVrMrTBVhTfamoAeQXP8UUgdLi1eOFEzo744sZeg67zYb7VImSRxEGMi1i6x5tNxyhS0CJEsbzFxZLjZLxxzDKR+fKWHJVvjJ8/xRantM+RoiGAE=",
        )
    ],
    model="claude-sonnet-4-5-20250929",
    parent_tool_use_id=None,
    error=None,
    usage={
        "input_tokens": 8,
        "cache_creation_input_tokens": 270,
        "cache_read_input_tokens": 21974,
        "cache_creation": {
            "ephemeral_5m_input_tokens": 270,
            "ephemeral_1h_input_tokens": 0,
        },
        "output_tokens": 4,
        "service_tier": "standard",
        "inference_geo": "not_available",
    },
    message_id="msg_013TztNJp6DuphjBTpdCsCtJ",
    stop_reason=None,
    session_id="8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    uuid="e4c49851-1cee-475a-9863-f190155dffad",
)
AssistantMessage(
    content=[
        TextBlock(
            text="Here's the current weather for both cities:\n\n🌁 **San Francisco**: Foggy, 62°F (17°C)\n- Classic San Francisco weather with fog rolling in!\n\n☀️ **Tokyo**: Clear, 68°F (20°C)\n- Beautiful clear skies in Tokyo!\n\nTokyo is a bit warmer and clearer right now, while San Francisco is experiencing its typical foggy conditions. Is there anything else you'd like to know about the weather or your travel plans?"
        )
    ],
    model="claude-sonnet-4-5-20250929",
    parent_tool_use_id=None,
    error=None,
    usage={
        "input_tokens": 8,
        "cache_creation_input_tokens": 270,
        "cache_read_input_tokens": 21974,
        "cache_creation": {
            "ephemeral_5m_input_tokens": 270,
            "ephemeral_1h_input_tokens": 0,
        },
        "output_tokens": 4,
        "service_tier": "standard",
        "inference_geo": "not_available",
    },
    message_id="msg_013TztNJp6DuphjBTpdCsCtJ",
    stop_reason=None,
    session_id="8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    uuid="89a2810a-85e0-4064-a99f-2e62fbac7ddc",
)
ResultMessage(
    subtype="success",
    duration_ms=13298,
    duration_api_ms=13842,
    is_error=False,
    num_turns=4,
    session_id="8a8865a9-5ac2-4933-9c1a-8af16df3f474",
    stop_reason="end_turn",
    total_cost_usd=0.03032875,
    usage={
        "input_tokens": 28,
        "cache_creation_input_tokens": 581,
        "cache_read_input_tokens": 65300,
        "output_tokens": 526,
        "server_tool_use": {"web_search_requests": 0, "web_fetch_requests": 0},
        "service_tier": "standard",
        "cache_creation": {
            "ephemeral_1h_input_tokens": 0,
            "ephemeral_5m_input_tokens": 581,
        },
        "inference_geo": "not_available",
        "iterations": [
            {
                "input_tokens": 8,
                "output_tokens": 147,
                "cache_read_input_tokens": 21974,
                "cache_creation_input_tokens": 270,
                "cache_creation": {
                    "ephemeral_5m_input_tokens": 270,
                    "ephemeral_1h_input_tokens": 0,
                },
                "type": "message",
            }
        ],
        "speed": "standard",
    },
    result="Here's the current weather for both cities:\n\n🌁 **San Francisco**: Foggy, 62°F (17°C)\n- Classic San Francisco weather with fog rolling in!\n\n☀️ **Tokyo**: Clear, 68°F (20°C)\n- Beautiful clear skies in Tokyo!\n\nTokyo is a bit warmer and clearer right now, while San Francisco is experiencing its typical foggy conditions. Is there anything else you'd like to know about the weather or your travel plans?",
    structured_output=None,
    model_usage={
        "claude-haiku-4-5-20251001": {
            "inputTokens": 511,
            "outputTokens": 15,
            "cacheReadInputTokens": 0,
            "cacheCreationInputTokens": 0,
            "webSearchRequests": 0,
            "costUSD": 0.0005859999999999999,
            "contextWindow": 200000,
            "maxOutputTokens": 32000,
        },
        "claude-sonnet-4-5-20250929": {
            "inputTokens": 28,
            "outputTokens": 526,
            "cacheReadInputTokens": 65300,
            "cacheCreationInputTokens": 581,
            "webSearchRequests": 0,
            "costUSD": 0.029742750000000002,
            "contextWindow": 200000,
            "maxOutputTokens": 32000,
        },
    },
    permission_denials=[],
    deferred_tool_use=None,
    errors=None,
    api_error_status=None,
    uuid="112ba6a2-a081-452a-9add-10eda6c7f2a0",
)


查看監控結果

執行完後在LangSmith網站上點選[Tracing]可看到此次執行的監控結果。





[TURN 1]即為此次程式執行的一輪對話。



點選左側選單的[Monitoring]可以看到一些統計數據。



沒有留言:

AdSense