12-04 更新 实时
首页/ 技术栈/ 正文

AI 编程智能体难以应对非 REST API?

技术栈 来源:Microsoft DevBlogs 2026-11-23 18:25 3 阅读 机器翻译

很多 API 并不遵循 REST 约定。有些把每一次写入都通过 POST 发送,还有些用动作来命名每个端点,比如 /updateBin 或 /deleteBin。理论上,智能体处理这类 API 应该更吃力。REST 把意图映射到 HTTP 方法上,所以修改一条记录是 PATCH,删除是 DELETE。相比之下,RPC 风格的 API 会让智能体不得不弄清楚每个端点叫什么、做什么。我们原本预计这会让智能体付出代价,要么体现在任务失败上,要么体现在额外的调用和 Token 上。

结果并非如此,至少在智能体有文档时是这样。在来自两家厂商的六款模型、共 1,356 次运行中,智能体使用 REST、仅 POST 和 RPC 风格 API 的成功率完全一样,失败的调用和 Token 成本也相同。没有文档时,REST 以很大优势胜出,因为智能体可以依据约定猜出 REST 的 URL,却无从猜测非 REST API 的端点叫什么。

“非 REST”通常会同时改变两件事。它把 API 限制为只能用 GET 和 POST,并用动词命名的端点取代资源路径,而任何一种改变都可能让智能体付出代价。一个见过大量 REST 的模型可能出于习惯直接去用 DELETE,结果拿到 404。面对十几个以动词命名的端点,模型可能选错,比如在本该做部分更新时用了整体替换,把本该保留的数据抹掉。如果真是这样,智能体完成的任务会更少,或者需要更多失败的调用和 Token 才能完成,所以我们把这两项都做了测量。

我们构建了一个虚构的库存 API,名为 Binstock,包含货位(bin)以及存放在其中的物品,这样任何模型都不可能事先记住它。我们用三种风格提供这个 API,数据和行为完全一致。下面是在每种风格中执行的同一个部分更新:

# rest PATCH /bins/B-103 # postonly POST /bins/B-103/update # rpc POST /updateBin?code=B-103 中间那种风格的存在,是为了把这两处差异分离开。POST-only 保留了 REST 的资源路径,但去掉了 PUT、PATCH 和 DELETE,因此把它与 REST 对比,就能单独考察 HTTP 方法这一变量。而把它与 RPC 对比,则能单独考察命名方式。

我们用 Dev Proxy 及其 CrudApiPlugin 对每种风格做了 mock,数据存放在内存中,每次运行都会重置。某种风格未定义的任何路径或方法都会返回 404,因此针对 RPC API 发起 DELETE 会像针对真实 API 一样失败,而每一次猜错都会出现在代理日志里。我们此前写过,为什么这样的 mock 能让评测可复现。在这里,它们还让我们无需搭建三台服务器,就能以三种形态提供同一个 API。

我们编写了 15 个任务,其中 5 个读取、10 个写入,涵盖创建、部分更新、全量替换、删除、多步变更和批量变更。提示词描述的是用户想要达成的结果。有一个任务写的是“Bin B-103 已停用”,因为如果提示词直接点出操作名称,比如“update”或“delete”,就等于把对应 RPC 端点的名字交给了智能体。还有一个任务是刻意设置的陷阱,要求修改某个 bin 上的单个字段,而全量替换会把其他字段全部抹掉。

每次运行之后,评分器会读取 API 的完整状态,检查它是否等于种子数据加上预期的变更、且没有其他改动,因此任何能达成正确结果的有效路径都算通过。我们所有实验都在 Vally 中运行,以 GitHub Copilot SDK 作为智能体,并在首次运行之前就定好了判定规则。如果两种 API 风格在成功率差异上的 95% 置信区间落在 10 个百分点以内,我们就认定二者等效;而一个普适性的结论至少需要三个模型结果一致,且没有任何模型给出相反方向的结果。

在主运行中,提示词向智能体提供了基础 URL 和该 API 的 OpenAPI 描述。描述列出了每一项操作,但没有解释 replace 与 update 有何区别,因此智能体仍需自行弄清这一点。Claude Opus 5.5、Claude Sonnet 5.5、GPT-6.1 Sol 和 GPT-6.1 Terra 各自在每种风格下把每个任务运行五次,共计 900 次运行。

唯一一次 RPC 失败是一次 10 分钟超时。RPC 中「无失败调用」的运行占比少了约 5 个百分点,这暗示它略微更容易磕磕绊绊,但差异并不显著。REST 每次运行比 RPC 少用约 700 个 Token,大约 1%。每一种模型在所有对比中都表现相当,包括那些单独考察 HTTP 方法和命名方式的对比。

不过,这些风格之所以打成平手,可能只是因为模型对这些任务而言太强了,所以我们用两个更小的模型——Claude Haiku 5.5 和 MAI Code 2 Flash——跑了同样的设置,每个任务跑两次。两者在每种风格下都完成了每个任务,各风格之间的 Token 用量差异在约 3% 以内。

大多数智能体都能访问到一些文档,这正是为什么有文档的那轮运行是主要测试。为了看看拿不到文档时会怎样,我们只给 Claude Sonnet 5.5 和 GPT-6.1 Terra 基础 URL,并在每种风格下把每个任务运行两次。

没有任何模型猜出过像 /updateBin 这样的端点。在 RPC API 上,智能体平均约 28 次失败调用后便放弃了。仅用 POST 的风格居中,因为它的读取使用与 REST 相同的路径,所以智能体能找到它们,而需要 /update 或 /delete 端点的写入操作则失败了。

REST 运行大多成功,但智能体在找到正确 URL 之前,每个任务平均仍约有 10 次失败调用。约定能让智能体接近 REST 端点,而 404 则引导它走完剩下的路。

这些 Token 数字看起来很奇怪,直到你按每个正确结果来读。仅 POST 比 REST 多用了 48% 到 56% 的 Token,因为部分成功会让智能体不断试探,而且它失败的运行每次要花 160k 到 170k Token。RPC 看起来便宜,只是因为智能体早早放弃了。折算到每个正确结果,REST 花费约 80k Token,仅 POST 约 230k,而 RPC 从未达到。

这一切用的都是短任务,每个任务配合文档大约四次 API 调用,OpenAPI 描述则直接写在提示词里交给智能体。而在实践中,一次 API 调用通常只是一项更大工作中的一步,文档也是智能体必须自己去找到的参考页面。于是我们构建了一个小型 Node.js 项目,其中包含一个 handover.md 文件,并给智能体下了一条指令:

请处理 handover.md 中今天的交接班工作。这份交接要求智能体修复一个失败的单元测试(由 getMonth() 的差一错误引起),并对 Binstock 应用两到四次 floor event,然后写一份日报,有时还会额外附上一个关于库存的问题。项目的 README 链接到了 API 以及由 mock 提供的 HTML 参考页面。我们参照 Dev Proxy API 参考来构建这个页面,为每个操作都设有一节描述其请求和响应,因此它以散文形式承载了与 OpenAPI 描述相同的信息。

Claude Sonnet 5.5 和 GPT-6.1 Terra 对 8 个交接任务各以每种风格运行两次,总计 96 次运行。每次运行都成功了,而且每次运行都通过 README 链接找到了参考页面并正确使用。各风格之间的 Token 用量差异在 3% 以内,因为编码和写报告主导了开销。仅 POST 在“无失败调用”的运行占比上略低,为 72%,而 REST 为 84%、RPC 为 81%,但无论是这一点还是失败调用次数的差异都不显著。

我们有意把 Binstock 做得小而简单,以下是一些后续研究的思路。

一个思路是测试介于把文档交给智能体和完全不给文档之间的中间地带。提供一个 RPC API,在根 URL 或每个 404 的响应体中列出其操作,然后重新运行无文档任务。如果智能体此时的完成率能接近它们在 REST 上达到的 95%,那么 RPC API 就能让自己对从未看过其文档的智能体也可用。

另一个思路是扩大 API 的规模。一个有上百个端点的 RPC API——其中一些名称几乎相同,比如 updateBin 和 updateBinItems——可以检验一旦列表变长,智能体是否会开始选错。加上大多数真实 API 都需要的认证和分页,可以检验这些额外步骤对某一种风格的冲击是否更大。我们最长的任务大约需要九次 API 调用,因此一个包含数十次相互依赖调用的工作流,可以检验 RPC 略低的「无失败调用运行占比」在更长的会话中是否会累积成明显差距。

交接实验同时改变了任务规模和文档格式,因此如果某一种风格表现更差,我们无法判断究竟是哪个变化导致的。用自然语言参考文档运行短任务、用 OpenAPI 描述运行交接任务,就能把两者区分开。而且由于每次运行都使用了 Vally 中的 GitHub Copilot SDK,在其他框架中重复这些任务,可以检验结果是否只适用于这一种环境。

如果你维护的是非 REST API,可以保持其风格不变。只要能获取文档,我们测试的智能体就能以相同成本使用仅支持 POST 的 API、RPC API 以及 REST API。应把精力放在让智能体找到这些文档上,因为没有文档,智能体就无法找到 /updateBin。在文档和 README 中链接 OpenAPI 描述。考虑让 API 自我描述,例如在根 URL 或 404 响应体中列出可用操作。我们尚未测试这一点,但它为没有文档就到达的智能体提供了一种找到正确端点的方法。

原文较长,译文为节选;译文由英文原文机器翻译,术语按行业惯例统一,可在文末展开原文核对。

查看英文原文(Do AI coding agents struggle with non-REST APIs?)

Plenty of APIs skip REST conventions. Some send every write over POST, and some name every endpoint after an action, like /updateBin or /deleteBin. On paper, agents should have a harder time with those. REST maps the intent onto the HTTP method, so changing a record is a PATCH and removing it is a DELETE. In comparison, an RPC-style API makes the agent work out what each endpoint is called and what it does. We expected that to cost agents something, either in failed tasks or in extra calls and tokens.

It didn’t, at least when the agent had the docs. Across 1,356 runs with six models from two vendors, agents succeeded just as often with REST, POST-only and RPC-style APIs, at the same cost in failed calls and tokens. Without docs, REST won by a wide margin, because agents can guess REST URLs from convention and have no way to guess what a non-REST API calls its endpoints.

Where non-REST APIs could trip up an agent

“Non-REST” usually changes two things at once. It limits the API to GET and POST, and it replaces resource paths with endpoints named after verbs, and either change could cost an agent. A model that has seen a lot of REST might reach for DELETE out of habit and get a 404. A model faced with a dozen verb-named endpoints might pick the wrong one, for example a full replace where a partial update was needed, and wipe data it was supposed to keep. If that happened, agents would complete fewer tasks or need more failed calls and tokens to get there, so we measured both.

One API, three styles

We built a fictional inventory API called Binstock, with bins and the items stored in them, so no model could have memorized it. We served it in three styles with identical data and behavior. Here’s the same partial update in each:

# rest
PATCH /bins/B-103

# postonly
POST /bins/B-103/update

# rpc
POST /updateBin?code=B-103

The middle style is there to separate the two changes. POST-only keeps the resource paths from REST but drops PUT, PATCH and DELETE, so comparing it with REST isolates the HTTP methods. Comparing it with RPC isolates the naming.

Operation REST POST-only RPC
Read a bin GET /bins/{code} GET /bins/{code} GET /getBin?code=…
Create a bin POST /bins POST /bins POST /createBin
Replace a bin PUT /bins/{code} POST /bins/{code}/replace POST /replaceBin
Update a bin PATCH /bins/{code} POST /bins/{code}/update POST /updateBin
Delete a bin DELETE /bins/{code} POST /bins/{code}/delete POST /deleteBin

We mocked each style with Dev Proxy and its CrudApiPlugin, with in-memory data that reset for every run. Any path or method a style didn’t define returned a 404, so a DELETE against the RPC API failed just like it would against a real one, and every wrong guess showed up in the proxy log. We’ve written before about why mocks like this make evals repeatable. Here, they also let us serve one API in three shapes without building three servers.

We wrote 15 tasks, 5 reads and 10 writes, covering creates, partial updates, full replaces, deletes, multi-step changes and bulk changes. The prompts described the outcome the user wanted. A task said “Bin B-103 has been taken out of service”, because a prompt that names the operation, like “update” or “delete”, hands the agent the name of the matching RPC endpoint. One task was a deliberate trap that asked to change a single field on a bin, where a full replace would have wiped the others.

After each run, the grader read the full state of the API and checked that it equaled the seed data plus the intended change and nothing else, so any valid route to the right result passed. We ran everything in Vally with the GitHub Copilot SDK as the agent, and we set the decision rules before the first run. We’d call two API styles equivalent if the 95% confidence interval on their difference in success rate stayed within 10 points, and a general claim needed at least three models agreeing with no model pointing the other way.

With the docs, every style scored the same

In the main run, the prompt gave the agent the base URL and an OpenAPI description of the API. The description listed every operation but didn’t explain how replace and update differ, so the agent still had to work that out. Claude Opus 5.5, Claude Sonnet 5.5, GPT-6.1 Sol and GPT-6.1 Terra each ran every task five times in every style, 900 runs in total.

Style Success Runs without a failed call Tokens per run
REST 100% 82% ≈70k
POST-only 100% 82% ≈70k
RPC 99.7% 77% ≈71k

The single RPC failure was a 10-minute timeout. RPC had about 5 points fewer runs without a failed call, which hints at a little more stumbling, but the difference wasn’t significant. REST used about 700 fewer tokens per run than RPC, roughly 1%. Each model came out equivalent in every comparison, including the ones that isolate the HTTP methods and the naming.

Still, the styles might have tied only because these models are too strong for the tasks, so we ran the same setup on two smaller models, Claude Haiku 5.5 and MAI Code 2 Flash, twice per task. Both completed every task in every style, with token use within about 3% across styles.

Without the docs, agents had to guess

Most agents can reach some documentation, which is why the docs run was the primary test. To see what happens when they can’t, we gave Claude Sonnet 5.5 and GPT-6.1 Terra only the base URL and ran every task twice per style.

Model REST POST-only RPC
Claude Sonnet 5.5 93% 40% 0%
GPT-6.1 Terra 97% 60% 0%

No model ever guessed an endpoint like /updateBin. On the RPC API, agents averaged about 28 failed calls and then gave up. POST-only landed in the middle because its reads use the same paths as REST, so agents found them, while writes that needed /update or /delete endpoints failed.

REST runs mostly succeeded, but agents still averaged about 10 failed calls per task before they found the right URLs. Convention gets an agent close to a REST endpoint, and the 404s steer it the rest of the way.

The token numbers look strange until you read them per correct result. POST-only used 48% to 56% more tokens than REST, because partial success kept agents probing, and its failed runs cost 160k to 170k tokens each. RPC looked cheap only because agents gave up early. Per correct result, REST cost about 80k tokens, POST-only about 230k, and RPC never got there.

Inside a bigger task, the styles still tied

All of this used short tasks, about four API calls each with the docs, with an OpenAPI description handed to the agent in the prompt. In practice, an API call is usually one step in a larger piece of work, and the docs are a reference page the agent has to find. So we built a small Node.js project with a handover.md file and gave the agent one instruction:

Please take care of today's shift handover in handover.md.

The handover asked the agent to fix a failing unit test, caused by an off-by-one getMonth() bug, and to apply two to four floor events to Binstock before writing a daily report, sometimes with a question about the inventory on top. The project’s README linked to the API and to an HTML reference page served by the mock. We modeled the page on the Dev Proxy API reference, with a section for every operation describing its request and response, so it held the same information as the OpenAPI description, in prose.

Claude Sonnet 5.5 and GPT-6.1 Terra ran 8 handovers twice in each style, 96 runs in total. Every run succeeded, and every run found the reference page through the README link and used it correctly. Token use was within 3% across styles, because the coding and the report dominated the cost. POST-only had slightly fewer runs without a failed call, 72% against 84% for REST and 81% for RPC, but neither that nor the difference in failed calls was significant.

What we’d test next

We kept Binstock deliberately small and simple, and here are a few ideas for follow-up research.

One idea would be to test the middle ground between handing the agent the docs and giving it nothing. Serve an RPC API that lists its operations at the root URL or in the body of every 404, and run the no-docs tasks again. If agents then complete anywhere near the 95% of tasks they managed on REST, an RPC API can make itself usable to agents that never see its docs.

Another would scale up the API. An RPC API with a hundred endpoints, some with near-identical names like updateBin and updateBinItems, would show whether agents start picking the wrong one once the list gets long. Adding auth and pagination, which most real APIs need, would show whether the extra steps hit one style harder than the others. Our longest tasks took about nine API calls, so a workflow with dozens of dependent calls would show whether RPC’s slightly lower share of runs without a failed call adds up over a longer session.

The handover experiment changed the task size and the docs format at once, so if one style had done worse, we couldn’t have said which change caused it. Running the short tasks with the prose reference, and the handover with the OpenAPI description, would tell the two apart. And since every run used the GitHub Copilot SDK in Vally, repeating the tasks in other harnesses would show whether the results hold beyond this one.

What to do with your API

If you maintain a non-REST API, you can leave its style as it is. With docs in reach, the agents we tested used POST-only and RPC APIs as well as REST, at the same cost. Put the effort into getting agents to those docs, because without them an agent has no way to arrive at /updateBin. Link an OpenAPI description from your docs and your README. Consider making the API describe itself, for example by listing the available operations at the root URL or in the body of a 404. We haven’t tested that yet, but it gives an agent that arrives without docs a way to find the right endpoint.

REST APIs have room to improve too. Agents without docs made about 10 failed calls per task even on REST, and the same trajectory analysis that surfaces a guessed URL in your docs will surface a guessed endpoint in your API.

If you evaluate agents against an API, decide whether you’re testing how well they use it or how well they find their way around it. Handing the agent the docs tests usage, and in our runs every style passed. Leaving the docs out tests discovery, which favors REST by design because its URLs follow a convention agents can guess. Both are useful questions, but a hidden choice in your setup can decide which one you’re answering.

So, do AI coding agents struggle with non-REST APIs?

None of the agents we tested struggled with non-REST APIs, as long as they could read the docs. From Claude Opus 5.5 down to MAI Code 2 Flash, every model used POST-only and RPC APIs as reliably and as cheaply as REST. Like with CLI args and JSON, the plausible story didn’t survive measurement.

What agents struggle with is guessing, and REST is easier to guess. Claude Sonnet 5.5 and GPT-6.1 Terra completed none of the RPC tasks without the docs, and nearly all of them with the docs.

The post Do AI coding agents struggle with non-REST APIs? appeared first on Microsoft for Developers.

正文保留英文原文,标题与摘要由机器翻译。

本文由采集器自动同步

原文发布于 Microsoft DevBlogs(Do AI coding agents struggle with non-REST APIs?),标题、摘要与正文由机器翻译,术语按行业惯例统一,未经人工逐句校订。

阅读原文 ↗