章鱼总结
00:00 / 11:53

Prompt Engineering · 11:53

Agents today are running hundreds of tools because every MCP server you connect to your agent brings a bunch of the tools.

如今,智能体要运行数百种工具,因为你连接到智能体的每个 MCP 服务器都会带来一大堆工具。

now, the problem is that every tool comes with a definition, a description, and its parameters, and all of them sit in the context window of your model.

问题在于,每种工具都有定义、描述和参数,而它们全都占据着模型的上下文窗口。

so even with million token context window, a big part of it goes to tool descriptions before the agent reads your task.

所以,即使上下文窗口有一百万个 token,在智能体读取你的任务之前,其中很大一部分也已经被工具描述占用了。

and the problem is that the tool selection accuracy drops as the list of tools grows.

而且,随着工具列表变长,工具选择的准确率会下降。

even with the frontier models that we have today.

即使是我们如今拥有的前沿模型也一样。

now, to deal with this, a lot of coding agents and agent frameworks now ship some form of code mode, where the agent writes a program that calls the tools, the program runs on its own, and only the results come back into the context window of the model.

为了解决这个问题,如今许多编程智能体和智能体框架都提供了某种形式的代码模式:智能体编写一个调用工具的程序,程序独立运行,只有结果才会返回模型的上下文窗口。

and this works great in practice.

这种做法在实践中效果很好。

it's one of the best solutions out there for dealing with context bloat.

这是目前解决上下文膨胀问题的最佳方案之一。

now, recently, google added something called tool retrieval to the gemini api, which lets the model search for the tool it needs and only load those.

最近,Google 在 Gemini API 中新增了一个名为工具检索的功能,模型可以借此搜索所需的工具,并且只加载那些工具。

on paper, it looks like an alternative to code mode, but when you combine the two, it becomes extremely powerful because each one fixes a different part of the problem.

从表面上看,它似乎是代码模式的替代方案;但把两者结合起来后,效果会非常强大,因为它们分别解决了问题的不同部分。

but before i explain how it works, let me show you a quick demo.

不过,在解释它的工作原理之前,先来看一个简短的演示。

so this is the same agent doing the same task three times, and it goes from more than half a million input tokens down to about 30,000.

这是同一个智能体执行同一项任务三次,输入 token 数从五十多万降到了约三万。

now google also released something called file slicing, which applies the same idea to big pdfs and spreadsheets.

Google 还发布了一项名为文件切片的功能,它把同样的思路应用到了大型 PDF 和电子表格上。

and we're going to cover that later in the video.

我们会在视频后面讲到这一点。

so by the end of this video, you will know what each one fixes, how to wire them into a single agent, and how to load only the part of a file that you need.

所以,看完这段视频,你就会知道每种方法分别解决什么问题、如何把它们接入同一个智能体,以及如何只加载文件中所需的部分。

okay, so let's first talk about what exactly code mode is and the problem it solves.

好,我们先来谈谈代码模式究竟是什么,以及它解决的问题。

when an agent uses tools the normal way, it wastes token in two different places.

智能体以常规方式使用工具时,会在两个不同的地方浪费 token。

the first one is the tool definition because every tool gets loaded up front on every request, whether the agent needs it or not.

第一处是工具定义,因为每次请求都会预先加载每种工具,不管智能体是否需要它。

the second one is the intermediate results because every tool result flows back through the model even when the model only needs one number from the entire context.

第二处是中间结果,因为每个工具的结果都会经过模型传回来,即使模型只需要整个上下文中的一个数字。

for example, if you ask the agent to count the open tickets from your top customers, it has to pull every page of the tickets into the context just to get the count.

例如,如果你让智能体统计头部客户未结工单的数量,它就必须把工单的每一页都调入上下文,才能得到这个数量。

so code mode fixes this second problem by exposing the tools as functions in code, which means the model writes the program that calls them.

因此,代码模式通过将工具作为代码中的函数公开,解决了第二个问题,这意味着模型会编写调用这些工具的程序。

the program runs in a sandbox, which is a separate lockdown environment, so all the loops, filters, and joins happen there instead in the model's context window.

程序在沙箱中运行。沙箱是一个独立的受限环境,因此所有循环、筛选和合并操作都在那里执行,而不是占用模型的上下文窗口。

only what the program actually print comes back to the model.

只有程序实际打印出来的内容才会返回给模型。

and instead of every page of a ticket, it gets those four numbers which we are looking for.

它拿到的不是工单的每一页,而是我们要找的那四个数字。

now, cloudflare calls this pattern code mode and Anthropic calls it code execution with mcp.

现在,Cloudflare 把这种模式称为代码模式,而 Anthropic 称之为使用 MCP 的代码执行。

so you will see both names thrown around.

所以你会看到这两种叫法都有人使用。

okay, so to write that program, the model still needs the exact names and parameters of the right tools, which means it has to find them first.

好,要编写这个程序,模型仍然需要知道正确工具的确切名称和参数,这意味着它必须先找到这些工具。

so every code mode setup builds its own discovery step.

因此,每种代码模式的配置都会构建自己的发现步骤。

for example, in Anthropic's version, it's the agent browse a folder of tool files.

例如,在 Anthropic 的版本中,代理会浏览一个存放工具文件的文件夹。

And Cloudflare added search and describe functions.

而 Cloudflare 添加了搜索和描述函数。

now, the discovery step is the part that google has built into the gemini api.

现在,发现步骤正是 Google 内置于 Gemini API 的部分。

so let's talk about how it works under the hood.

接下来,我们来聊聊它在底层是如何运作的。

the setup is small because you add one entry called tool search to your list of tools.

配置很简单,因为你只需在工具列表中添加一个名为 tool search 的条目。

and you mark the tools you want to hold back with a flag called defer loading.

然后,为你想暂缓加载的工具设置一个名为 defer loading 的标记。

for those deferred tools, the model only sees each tool's name and a short description, so it knows which tools exist, but it doesn't see their parameters until it asks for them.

对于这些延迟加载的工具,模型只能看到每个工具的名称和简短描述,因此它知道有哪些工具,但在请求之前看不到它们的参数。

when the model needs a tool, three things happen inside one api request, so your app doesn't need any extra round trips.

当模型需要使用某个工具时,一次 API 请求中会发生三件事,因此你的应用不需要额外的往返请求。

first, the model makes a tool search call with the names of the function it wants.

首先,模型会发起工具搜索调用,并提供它想使用的函数名称。

then the api returns a tool search result, which loads the full definition of those tools into the conversation.

然后,API 会返回工具搜索结果,将这些工具的完整定义加载到对话中。

after that, the model makes a formal function call, which your app runs as usual.

之后,模型会发起正式的函数调用,你的应用照常执行它。

so both these approaches are great.

所以,这两种方法都很不错。

however, you don't need them in every situation.

不过,并非在所有情况下都需要它们。

so for example, according to google stocks, if you have fewer than 20 tokens that don't change much, loading them all up front is actually a better fit.

例如,根据 Google 的数据,如果你要用的令牌少于 20 个,而且变化不大,那么一开始就把它们全部加载进来其实更合适。

and with a small library, it can even cost less.

而且对于规模较小的库,这样甚至可能更省钱。

and this is because how prompt caching works.

这是因为提示缓存的工作方式就是如此。

so i would recommend keeping the tools you use on almost every request loaded and defining the long tail.

因此,我建议将几乎每次请求都会用到的工具保持加载状态,并对那些不常用的工具进行定义。

so the easiest way to think about the two together is the tool that will decide which tools the model knows about and code mode decides where the work actually happens.

简单来说,要理解这两者的配合方式:工具搜索决定模型能用到哪些工具,而代码模式决定实际工作在哪里进行。

okay, so now let's talk about how you wire those two together.

好了,现在我们来聊聊如何把这两者连接起来。

because in my setup, all the tools are different.

因为在我的设置中,所有工具都各不相同。

and one extra tool called run code is always loaded, which runs a python program in a sandbox.

另外还有一个名为 run code 的工具,它始终处于加载状态,可以在沙盒中运行 Python 程序。

so the tool uses tool search to load the few tools it needs, then it writes a program against them, and the sandbox makes all the api calls.

因此,模型会使用工具搜索加载所需的几个工具,然后针对这些工具编写程序,再由沙盒发起所有 API 调用。

now the part that makes this work is a short instruction in the system prompt, which is the standing instruction the model gets on every turn.

让这一切得以实现的,是系统提示中的一条简短指令,也就是模型每一轮都会收到的常驻指令。

and it tells the model to read the parameters with tool search, but to put the calls in a program instead of calling the tools directly.

它告诉模型使用工具搜索读取参数,但要把调用写进程序里,而不是直接调用工具。

because without the instruction, nothing stops the model from calling the loaded tools one at a time, which brings the raw data back into the context.

因为如果没有这条指令,就没有什么能阻止模型逐个调用已加载的工具,而这会把原始数据带回上下文中。

Now, a model written code needs a real sandbox.

现在,模型编写的代码需要一个真正的沙盒。

For this demo, we're using a demo one, which is a separate Python process with a timeout.

这个演示中,我们用的是一个演示用沙盒,它是一个带超时机制的独立 Python 进程。

But for production, I would highly recommend a properly isolated environment, for example, a dedicated container.

但在生产环境中,我强烈建议使用经过妥善隔离的环境,例如专用容器。

okay, so that's enough theory.

好了,理论部分就讲到这里。

let me show you this in action.

让我演示一下实际效果。

so the task at hand is the same in all three runs, which is to count the open tickets from the gold tier customers in Europe by priority and post the count to a slack channel.

三次运行的任务都相同:按优先级统计欧洲黄金级客户的未结工单数量,并将统计结果发送到一个 Slack 频道。

the company and its data are made up, so i know the right answers.

这家公司及其数据都是虚构的,所以我知道正确答案。

and the agent has to join two apis.

而且代理必须将两个 API 的结果合并。

that each return one page of records at a time.

它们每次各自只返回一页记录。

So while these runs, I want to watch two things, which are the context meter and whether the final Slack post has the right counts.

运行期间,我想关注两件事:上下文用量,以及最终发布到 Slack 的数量是否正确。

In the first run, all tools are loaded.

第一次运行时,所有工具都已加载。

So before the agent does anything, it carries about 26,000 tokens of tool definitions.

所以代理还没开始做任何事,上下文里就已经有大约 26,000 个 token 的工具定义。

Then every page of tickets comes back into its context.

然后,每一页工单都会进入它的上下文。

So it does post the right count, but it uses about 500and 64,000 input tokens to get there.

它确实发布了正确的数量,但为此用了大约 564,000 个输入 token。

now in the second run, the tools are deferred, so the first request drops to a few thousand tokens, and the agent finds the tool with the tool search.

第二次运行时,工具采用了延迟加载,因此首次请求的 token 数降到了几千,代理通过工具搜索找到了所需工具。

but the customer and the ticket pages still flow through the model's context, so by the end it used about 326,000 input tokens.

但客户数据和工单页面仍会流经模型的上下文,所以到最后,它用了大约 326,000 个输入 token。

so tool retrieval on its own fixed the definition, but it didn't touch the intermediate results.

因此,仅靠工具检索解决了工具定义的问题,却没有处理结果数据。

now in the third run, the agent has tool retrieval and the run code tool together.

第三次运行时,代理同时使用了工具检索和运行代码工具。

it searched once and loaded five tool definitions, then it wrote a few short programs, one to explore the data, one to count the tickets, and one to post to Slack.

它搜索了一次并加载了五个工具定义,然后编写了几个简短的程序:一个用于探索数据,一个用于统计工单数量,还有一个用于发布到 Slack。

so 43 api calls ran inside the sandbox and only a few printed lines came back to the model.

于是,43 次 API 调用都在沙盒中运行,只有几行打印结果返回给模型。

so the whole run used about 30,000 input tokens with the same correct count because tool retrieval kept the definitions out of the context and the code mode kept the raw data out of it.

因此,整个运行只用了大约 30,000 个输入 token,同时得出了相同的正确数量:工具检索让工具定义不必进入上下文,而代码模式则让原始数据不必进入上下文。

now these are single runs from my machine so the exact numbers will change.

这些只是我在自己机器上进行的单次运行,所以具体数字会有所不同。

with your tools and your data.

换成你的工具和数据,结果也会不同。

now let me show you the counting program, the model wrote, because this is where the savings come from.

现在我来给你看看模型写的统计程序,因为节省的 token 就是从这里来的。

it wrote a small helper that walks through every page of the results, then it filtered the customer down to gold tier in europe, matched their tickets and counted them by priority.

它写了一个小辅助程序,遍历结果的每一页,然后筛选出欧洲的金卡客户,匹配他们的工单,并按优先级统计数量。

it even double-checked its answer by asking the api for the same filters a second way.

它还通过另一种方式再次向 API 请求相同的筛选结果,来复核答案。

and the model only saw the counts.

而模型看到的只有统计数量。

okay, so this was a quick way of combining both of these powerful approaches together to get the best of both worlds.

好,这就是一种快速将这两种强大的方法结合起来、兼得两者优势的方式。

now, let's talk about file slicing, which is the other release i mentioned at the start.

现在,我们来谈谈文件切片,这是我在开头提到的另一项发布内容。

now, it's the same idea applied to files because instead of sending the whole pdf or spreadsheet to the model, you tell the api which pages or cells to load.

它的思路相同,只是应用于文件:你不必把整份 PDF 或电子表格发送给模型,而是告诉 API 要加载哪些页面或单元格。

for example, i made a long annual report.

例如,我制作了一份很长的年度报告。

this is just made up with an answer is a number inside a chart on one page.

这只是一个虚构的例子,答案是某一页图表中的一个数字。

the agent first reads the content page.

智能体先读取了目录页。

it found the chart and loaded just the page as an image, which used a few thousand tokens instead of more than 200,000 tokens for the whole pdf.

它找到了图表,并且只将那一页作为图像加载,耗费了几千个令牌,而不是处理整份 PDF 所需的 20 多万个令牌。

now the same works for spreadsheets where you can load one tab.

同样的方法也适用于电子表格,你可以只加载一个工作表。

and a cell range instead of the whole workbook.

再加上一个单元格区域,而不必加载整个工作簿。

but a text only slice cannot see a chart.

但纯文本切片无法识别图表。

so if your answer is inside a figure, you need to load that page as an image.

因此,如果答案位于图表中,你需要将那一页作为图像加载。

now, it's more important to know the limitations.

现在,更重要的是了解它的局限性。

the first one is that the tool retrieval is in preview on the interactions api only.

第一,工具检索功能目前仅在 Interactions API 中处于预览阶段。

the second one is that once a tool is loaded, it stays loaded for the rest of the conversation.

第二,工具一旦加载,就会在对话的剩余部分中一直保持加载状态。

and you have to send the full list of tools on every turn.

而且每一轮都必须发送完整的工具列表。

and the last one, according to the docs, is that you should not defer any tool that you name in your system prompt.

最后,根据文档,你不应延迟加载系统提示词中提到的任何工具。

now, on the code mode side, i would recommend an approval step for any tool that changes something, things like posting a message or updating a record.

至于代码模式,我建议对任何会改变内容的工具都设置审批步骤,例如发布消息或更新记录。

i would also prefix every tool name with its services.

我还建议在每个工具名称前加上对应的服务名称。

things like crm or support and have your program print only those things that the model needs.

例如 CRM 或支持服务,并让程序只输出模型需要的内容。

the answer is that the tool retrieval gives code mode the discovery step it was missing.

总而言之,工具检索为代码模式补上了它原本缺少的发现步骤。

the model loads only the tool it needs and the code mode keeps the results out of the context.

模型只加载它需要的工具,而代码模式会将结果排除在上下文之外。

so if your agent has a lot of tools, i would start by defining them and if you're.

所以,如果你的智能体有很多工具,我会先从定义这些工具开始;如果……

task moves a lot of data, i would add code mode on top, at least based on my experiments.

任务需要处理大量数据,我会在此基础上加入代码模式,至少根据我的实验是这样。

the code for the demo agent is going to be in the video description.

演示智能体的代码会放在视频简介里。

and if you're handling tool discovery in your own agents in different way, please share in the comment section below.

如果你在自己的智能体中用不同的方式处理工具发现,请在下方评论区分享。

Now, if you want to learn more about how agent harnesses work, watch this video.

如果你想进一步了解智能体运行框架是如何工作的,请观看这段视频。

Anyways, I hope you found this video useful.

总之,希望这段视频对你有所帮助。

Thanks for watching and as always, see you in the next one.

感谢收看,一如既往,我们下期再见。

摘要

连接大量 MCP 工具时,工具定义会占用上下文,逐页返回的中间数据也会进一步推高 token 消耗,并可能降低工具选择准确性。视频说明 Code Mode 如何让程序在沙箱中调用工具、处理数据,只把必要结果交回模型;Gemini 工具检索则按需加载工具定义。两者结合后,演示任务的输入 token 从约 56.4 万降至约 3 万,同时得到正确计数。视频还展示 PDF 与电子表格切片的用法,并讨论工具检索的适用情形、限制及沙箱安全建议。

章节

00:00 大量工具带来的上下文负担

一个智能体连接多个 MCP 服务器后,可能面对数百个工具。每个工具的定义、描述和参数都会进入模型上下文;即使上下文窗口很大,工具说明也会在模型读取任务前占据大量空间。工具列表越长,模型选择正确工具的准确性还可能下降。

Agent连接大量工具
工具定义与参数占用模型上下文
工具描述占据大量上下文窗口
工具列表增长伴随选择准确率下降

00:32 Code Mode 与工具检索分别解决什么问题

Code Mode 让模型编写调用工具的程序,并在独立环境中执行循环、筛选和连接等操作,只将程序输出带回上下文,从而减少中间数据的输入。Gemini 工具检索则让模型搜索所需工具并仅加载相应定义。两者解决的问题不同,结合使用有望同时减少工具定义和原始结果造成的上下文膨胀;演示任务中,同一智能体的输入 token 从超过 50 万降至约 3 万。

Code Mode 中由程序调用工具并返回结果的流程
Gemini API 按需搜索并加载工具
Code Mode 与工具检索各自解决不同问题

01:58 Code Mode 如何减少中间结果

常规工具调用会让模型承担两类开销:每次请求预先加载工具定义,以及让每项工具结果都经过模型上下文。比如统计重要客户的未结工单数量,常规方式可能把每一页工单都带回模型;Code Mode 则让模型编写程序,在沙箱中完成筛选和汇总,只打印所需数字。编写程序前仍须先发现正确工具及其参数,因此 Code Mode 配置还需要工具发现步骤。

概括代码模式只将程序输出返回给模型

03:51 Gemini 工具检索的工作方式与适用情况

配置时加入 tool search,并对希望延迟加载的工具设置 defer loading。模型最初只看到这些工具的名称和简短描述;需要某个工具时,会在同一 API 请求中搜索工具、载入完整定义,再发起正式函数调用。工具检索并非所有情况都更合适:少于 20 个且变化不大的工具可能适合预先加载,小型工具库也可能成本更低。较合适的做法是常用工具常驻,把长尾工具延迟加载。

延迟加载工具时模型可见的信息
搜索结果将工具完整定义加载到对话中
少量且稳定的工具适合预先加载
工具检索与代码模式各自负责的环节

05:27 把工具检索接入 Code Mode

示例架构将各项业务工具设为可检索工具,并始终加载一个在沙箱中运行 Python 程序的 run code 工具。模型先用工具检索读取所需工具的参数,再把调用写进程序,由沙箱执行 API 请求。系统提示必须明确要求模型通过程序调用工具,否则它可能逐个直接调用,令原始数据重新进入上下文。演示使用带超时的独立 Python 进程;生产环境建议采用隔离更充分的环境,例如专用容器。

工具检索、编写程序并由沙箱执行 API 调用的核心流程

06:38 同一任务的 token 消耗对比

演示任务是统计欧洲黄金等级客户的未结工单,并按优先级计数后发到 Slack;客户和工单 API 都分页返回数据。预加载全部工具时,工具定义约占 2.6 万 token,逐页数据经过模型上下文,最终约用 56.4 万输入 token。只启用工具检索后,初始开销降至几千 token,但页面数据仍流经模型,最终约用 32.6 万。两者结合后,模型搜索并加载五个工具定义,编写探索数据、统计工单和发送 Slack 消息的程序;43 次 API 调用在沙箱内运行,只返回少量结果,最终约用 3 万输入 token,计数仍正确。这些数字来自单次演示,实际结果会随工具和数据变化。

票据分页进入上下文
第一轮正确计数所用输入 token
第二轮输入 token 消耗
第三轮输入 token 消耗及正确计数
程序筛选客户并按优先级计数

09:26 按需读取 PDF 与电子表格内容

文件切片让模型只读取所需页面或单元格,而非整个文件。演示中的年度报告答案位于图表页:智能体先读目录,再把相关页面作为图像加载,使用几千 token,而整份 PDF 会超过 20 万 token。电子表格也可以只加载一个工作表和指定单元格区域;但纯文本切片无法识别图表,答案若位于图像中,就需要加载对应页面图像。

年报中图表所在页面
只加载图表所在页面为图像,并与整份 PDF 的 token 用量对比
电子表格中按单元格范围读取内容

10:22 功能限制与实施建议

工具检索当时仅在 Interactions API 预览版中提供;工具一旦载入,会在本次对话剩余时间保持载入状态,而且每轮仍须发送完整工具列表。文稿还指出,按文档建议,不应延迟加载系统提示中点名的工具。对于会改变数据或发布消息的工具,应加入审批步骤;工具名可加 CRM、support 等服务前缀,并让程序仅输出模型真正需要的信息。总体上,工具多时可先采用工具检索;任务还处理大量数据时,再叠加 Code Mode。

工具检索补足 code mode 缺少的发现步骤

结论

08:10 工具检索减少进入上下文的工具定义,Code Mode 减少进入上下文的原始工具结果,两者结合能够同时处理这两类开销。

07:27 在演示任务中,结合工具检索与 Code Mode 后,输入 token 从约 56.4 万降至约 3 万,且统计结果正确。

09:31 处理大型文件时,只加载需要的页面、工作表或单元格区域,可以避免把整份文件都放入模型上下文;图表内容则需要以图像方式读取。

06:24 会产生副作用的工具调用应设置审批步骤,Code Mode 也应运行在隔离的沙箱环境中。