Athropic Agent SDK 工具schema 占用tokens情况

使用 Agent tool, 并搭配一个自定义 sub agent

async def main():
    async for message in query(
        prompt="帮我将这句中文翻译成英文:\"无尽的冬夜\"",
        options=ClaudeAgentOptions(
            permission_mode="acceptEdits",
            model='claude-sonnet-4-6',
            system_prompt="你是一个机器人助理,能帮主人处理各种事情,如果需要翻译,请交给 fast-translater, 并且无条件使用它返回的结果。",
            skills=[],
            tools=['Agent'],
            agents={
                "fast-translater": AgentDefinition(
                    description="完成任意语言之间的翻译工作",
                    prompt="""你是一个爱开玩笑的翻译助手,任何翻译指令你都回答 "I don't know~ " """,
                    # tools=['Read'],
                )
            },
        ),
    ):
        print(message)

共 3982 total input tokens

{'input_tokens': 3,
 'cache_creation_input_tokens': 497,
 'cache_read_input_tokens': 3482,
 'cache_creation': {'ephemeral_5m_input_tokens': 0,
  'ephemeral_1h_input_tokens': 497},
 'output_tokens': 0,
 'service_tier': 'standard',
 'inference_geo': 'not_available'}

设置 tools = ['Agent', 'Read'],结果出乎预料,total input tokens反而变少了,还命中了缓存。

{'input_tokens': 3,
 'cache_creation_input_tokens': 3472,
 'cache_read_input_tokens': 0,
 'cache_creation': {'ephemeral_5m_input_tokens': 0,
  'ephemeral_1h_input_tokens': 3472},
 'output_tokens': 0,
 'service_tier': 'standard',
 'inference_geo': 'not_available'}

去掉 Agent 工具和 sub agent 定义,只保留 Read 工具

async def main():
    async for message in query(
        prompt="帮我将这句中文翻译成英文:\"无尽的冬夜\"",
        options=ClaudeAgentOptions(
            permission_mode="acceptEdits",
            model='claude-sonnet-4-6',
            system_prompt="你是一个机器人助理,能帮主人处理各种事情。",
            skills=[],
            tools=['Read'],
        ),
    ):
        print(message)

这次 total input tokens 是 2541, 至少省了1000左右的tokens

{'input_tokens': 3,
 'cache_creation_input_tokens': 2538,
 'cache_read_input_tokens': 0,
 'cache_creation': {'ephemeral_5m_input_tokens': 0,
  'ephemeral_1h_input_tokens': 2538},
 'output_tokens': 0,
 'service_tier': 'standard',
 'inference_geo': 'not_available'}

设置 tools=["Read", "Write", "Bash", "Task"], total token是7919

{'input_tokens': 3,
 'cache_creation_input_tokens': 7916,
 'cache_read_input_tokens': 0,
 'cache_creation': {'ephemeral_5m_input_tokens': 0,
  'ephemeral_1h_input_tokens': 7916},
 'output_tokens': 0,
 'service_tier': 'standard',
 'inference_geo': 'not_available'}
posted @ 2026-06-03 11:41  RolandHe  阅读(8)  评论(0)    收藏  举报