当一个 Agent 同时加载成千上万工具或者 MCP 的,上下文会爆炸,每次请求都会消耗大量的 Token,这时候最好的方式是按需加载工具,从而减少 Token 消耗 。 Proxy Tool 就是为解决这个问题而生 Agent 只需要一个 Proxy Tool,其会按需加载其他工具,通过 Proxy Tool 调用其他工具,从而大量减少上下文消耗。
基础原理
Proxy Tool 提供两个 Action :
- load:加载工具的描述信息和出参和入参的结构
- call:通过工具的名称执行指定的工具
Proxy Tool 同时支持 Hawa Code 内置工具和 MCP 扩展工具。
Load
使用 Hawa Code 使用 load 加载工具的描述信息,注意没有使用 json schema ,使用 typescript 格式更加节省 Token 。
# Read Reads a file from the local filesystem. You can access any file directly by using this tool. Assume this tool is able to read all files on the machine. If the User provides a path to a file assume that path is valid. It is okay to read a file that does not exist; an error will be returned. ## Input (TypeScript) ```typescript type ReadInput = { /** The absolute path to the file to read */ file_path: string; /** The line number to start reading from. Defaults to reading from the first line. Only provide if the file is too large to read at once. */ offset?: number; /** The number of lines to read. Defaults to reading until the last line of the file. Only provide if the file is too large to read at once. */ limit?: number; };
Call
通过被代理的 Tool 的名称和参数执行工具调用。
"input": { "action": "call", "input": { "file_path": "D:\\Javaworks\\hawa-code\\README.md" }, "name": "Read" },
Prompt Cache
模型都支持 Prompt Cache 功能,就是当前请求的前缀不变的情况下,可以复用 KV Cache,后续请求可以避免前缀的计算。
Anthropic Claude 的前缀格式如下:
[Tool Definitions] + [System Prompt] + [Conversation History...]
如果工具列表发生变化,就会导致整个前缀失效,Proxy Tool load 加载的内容是直接放到历史消息中的,并没有影响前缀任何内容。Prompt Cache 不会收到任何影响。
Hawa Code load 模式为了最大化减少 Token 会导致前缀变化。
Proxy Tool vs Code Mode
Hawa Code Proxy Tool 和 Code Mode 都可以做到工具按需加载,支持上万数量数量工具数量,在使用场景上两者是互补关系。
- Proxy Tool 是单个工具调用,无法做到多个工具级联调用。
- Code Mode 更加注重多个工具的级联调用,一个工具就是一个 API,就像写代码一样进行工具调用。
Hawa Code 主要提供 Proxy Tool 和 Code Mode 两个工具就可以实现所有功能