使用 Claude 平台降低成本并提升性能
Tuning prompt caching, instructions, and effort can reduce Claude's cost without sacrificing application performance.调整提示缓存、指令和努力程度,可以在不牺牲应用性能的前提下降低 Claude 的使用成本。
Tuning prompt caching, instructions, and effort can reduce Claude's cost without sacrificing application performance.调整提示缓存、指令和努力程度,可以在不牺牲应用性能的前提下降低 Claude 的使用成本。
Performance and cost are often viewed as a trade-off: to spend less, you accept worse results. In practice, we've found that many applications using Claude Platform can cut costs without giving up performance with three fixes: maximize the prompt cache hit rate, remove anti-patterns from your prompts when upgrading to frontier Claude models, and calibrate effort to the task. We've put this guidance into the claude-api skill. In this article, we show how Claude Code with the claude-api can often find ways to reduce cost while maintaining or improving performance.性能和成本通常被视为一种权衡:为了花更少的钱,你接受更差的结果。在实践中,我们发现许多使用 Claude 平台的应用可以通过三个调整来削减成本而不牺牲性能:最大化提示缓存命中率,在升级到前沿 Claude 模型时从提示中移除反模式,以及根据任务校准努力程度。我们已将这些指导整合到 claude-api 技能中。在本文中,我们展示了带有 claude-api 的 Claude Code 如何经常找到降低成本同时保持或提升性能的方法。
Before Claude generates a response, it first processes your prompt into an internal working state. This step, called prefill, is the expensive part of handling input. Prompt caching saves that state (the key–value, or KV, cache): when a request starts with the same prefix, Claude reads it back instead of recomputing it. Cache reads are billed at a fraction of the full input price.在 Claude 生成响应之前,它首先将你的提示处理成一种内部工作状态。这个称为预填充(prefill)的步骤是处理输入中开销最大的部分。提示缓存会保存该状态(即键值缓存,KV 缓存):当请求以相同前缀开始时,Claude 会读回它,而不是重新计算。缓存读取的计费仅为完整输入价格的一小部分。
There are a few practical considerations to ensure effective use of the prompt cache. First, the prompt cache is pinned to a specific model. Second, prompt cache reads must be byte-exact across the full span of the prompt. Finally, the prompt cache has a limited time-to-live (TTL).为确保有效使用提示缓存,有几个实际注意事项。首先,提示缓存绑定到特定模型。其次,提示缓存读取必须在整个提示范围内字节完全一致。最后,提示缓存的生存时间(TTL)是有限的。
With these points in mind, there are a few practical tips:考虑到这些要点,这里有一些实用技巧:
We’ve accumulated a few lessons for prompt cache management: 我们积累了一些提示缓存管理的经验:


Prompts can accumulate instructions that patch model weaknesses. These instructions can drift relative to the capabilities of the latest Claude models. Here are common prompting “anti-patterns” that hobble frontier Claude model and can inadvertently increase costs:提示可能会累积一些用来修补模型弱点的指令。这些指令会与最新 Claude 模型的能力产生偏差。以下是一些常见的提示“反模式”,它们会妨碍前沿 Claude 模型,并可能无意中增加成本:
We've updated the claude-api skill with a new command that watches out for these anti-patterns. In Claude Code, run /claude-api prompt-audit against your prompts, skills, or tool descriptions. The audit covers anything in your working directory, including application code that calls the Claude API and Claude Code's own configuration (e.g., CLAUDE.md or skills).我们已更新 claude-api 技能,新增了一个命令来识别这些反模式。在 Claude Code 中,对提示、技能或工具描述运行 /claude-api prompt-audit。该审计覆盖工作目录中的任何内容,包括调用 Claude API 的应用程序代码以及 Claude Code 自身的配置(例如 CLAUDE.md 或 skills)。
For example, we tested a model migration from Opus 4.8 to Opus 5 on a customer support benchmark. We started from a clean prompt and planted one anti-pattern at a time (a retired thinking setting, a pair of contradictory refund rules, a manual scratchpad, "verify twice", "be maximally thorough", and a mandatory six-step procedure), giving six legacy prompts. 例如,我们在一个客户支持基准上测试了从 Opus 4.8 到 Opus 5 的模型迁移。我们从一个干净的提示开始,每次植入一个反模式(一个已停用的思考设置、一组矛盾退款规则、一个手动草稿、“验证两次”、“尽量做到最全面”以及一个强制的六步流程),从而生成了六个旧提示。
We ran each on Opus 4.8, on Opus 5 with only the model ID changed, and on Opus 5 after running /claude-api prompt-audit once per prompt (Figure 3 shows the average across the six).我们分别使用 Opus 4.8、仅更改模型 ID 的 Opus 5,以及在每个提示上运行一次 /claude-api prompt-audit 后的 Opus 5 来运行每个提示(图3显示了六个提示的平均结果)。

Figure 3 | The effect of prompting anti-patterns during model migration from Opus 4.8 to Opus 5.图3 | 在从 Opus 4.8 迁移到 Opus 5 期间,提示反模式的影响。
With Opus 5, verification rituals ("verify twice") use unnecessary tokens by duplicating order lookup on every refund. Emphasis boosters ("be maximally thorough") became dozens of unneeded knowledge-base searches. 使用 Opus 5 时,验证仪式(“验证两次”)通过每次退款重复查询订单而浪费不必要的 token。强调词(“尽量做到最全面”)则导致几十次多余的知识库搜索。
Running /claude-api prompt-audit removed the anti-patterns, decreasing costs by 14.6% and increasing accuracy by 5.3% on average. Cost dropped because extra tool calls and duplicated reasoning were eliminated. Accuracy rose for three reasons. The retired thinking setting made the API reject every routing request outright. The contradictory refund rules led Opus 5 to withhold four refunds it owed while it asked the customer to confirm. And the manual scratchpad collided with Opus 5's built-in thinking: on three tickets it wrote the tool call inside its reasoning and never executed it.运行 /claude-api prompt-audit 可移除这些反模式,使成本平均降低 14.6%,准确率平均提升 5.3%。成本下降是因为消除了多余的调用和重复推理。准确率上升有三个原因:已停用的思考设置导致 API 直接拒绝所有路由请求;相互矛盾的退款规则使 Opus 5 在要求客户确认的同时,扣下了四次本应执行的退款;手动草稿与 Opus 5 的内置思考发生冲突:在三个工单中,模型将工具调用写在其推理内容中,却从未执行该调用。
Effort tells Claude “how hard to work.” At low effort Claude generally reaches conclusions faster. At high effort, Claude deliberates, verifies, and explores alternatives before answering. 努力程度告诉 Claude '应该多努力地工作'。在较低努力程度下,Claude 通常能更快得出结果。在较高努力程度下,Claude 在回答前会进行深思熟虑、验证并探索替代方案。
Cost-versus-performance across effort levels on a single model can vary. For example, Claude Fable 5 scores 11.5% at low effort for $5.35 per task on FrontierCode Diamond (the hardest 50 tasks). At max effort, Fable 5 gets 30.9% for $19.00 per task; changing effort raises the score about 2.7x (+19 points) for about 3.5x the cost (Figure 4). 仅改变努力程度所带来的成本与性能关系,在同一模型上也会有所不同。例如,Claude Fable 5 在 FrontierCode Diamond(最难的 50 个任务)上,低努力程度下得分为 11.5%,每个任务成本 5.35 美元;在最大努力程度下,Fable 5 得分 30.9%,每个任务成本 19.00 美元;更改努力程度可将得分提升约 2.7 倍(+19 分),成本增加约 3.5 倍(图4)。
On Claude Fable 5.1, Humanity's Last Exam (without tools) shows a steep curve with a diminishing last step. It scores about 53% at low effort for about $0.30 per question and about 61% at max effort for about $2.23; the last step up to max adds about half a point for 46% more cost. The gain inside the benchmark's run-to-run noise, so you pay more for no measurable gain.在 Claude Fable 5.1 上,Humanity's Last Exam(无工具)显示出陡峭的曲线,但最后一步收益递减。低努力程度下得分约为 53%,每道题成本约 0.30 美元;最大努力程度下得分约为 61%,每道题成本约 2.23 美元;从上一档提升到最大一档仅增加约 0.5 分,成本却增加 46%。该提升落在基准测试逐次运行的噪声范围内,因此你多花了钱却没有可衡量的收益。

Effort can be miscalibrated in either direction:努力程度可能在两个方向上校准不当:
There are some useful ways to calibrate effort: 有一些有用的校准努力程度的方法:

This calibration often involves running an evaluation across models and effort levels. In Claude Code, /claude-api hillclimb performs this search for you: it splits your evaluation into train and test sets, proposes configuration changes, and reads failing train examples to fix what it finds.这种校准通常涉及跨模型和努力程度运行评估。在 Claude Code 中,/claude-api hillclimb 可以帮你执行这种搜索:它会把评估拆分为训练集和测试集,提出配置更改建议,并通过查看失败的训练示例来修复发现的问题。
We ran it on a customer support benchmark, starting from Opus 4.8 at its default (high) effort. The hillclimber first tried Opus 5 at low effort, applying prompt-audit to remove mandatory tool-call rituals, scratchpad steps, and contradictory rules. That cleared the Opus 4.8 baseline at 98.9% train accuracy and cut cost to 2.6 cents per ticket.我们在一个客户支持基准上运行了它,从 Opus 4.8 默认(高)努力程度开始。hillclimber 首先尝试了低努力程度的 Opus 5,应用 prompt-audit 移除强制的工具调用仪式、草稿步骤和矛盾规则。这使模型达到了 Opus 4.8 基线的 98.9% 训练准确率,并将成本降至每张工单 2.6 美分。

It then stepped down to Sonnet 5 at low effort, which was cheaper still at 1 cent per ticket, but accuracy fell to 88.9%. Reading the failing train tickets, Claude added routing rules and a refund-cap cross-reference to the prompt, bringing Sonnet 5 back to 98.9% at the same cost.随后,它降低到低努力程度的 Sonnet 5,每个工单成本仅 1 美分,更加便宜,但准确率降至 88.9%。通过查看失败的训练工单,Claude 在提示中加入了路由规则和退款上限交叉引用,使 Sonnet 5 在相同成本下恢复到 98.9% 的准确率。
On the 14 held-out tickets the search never saw, the final configuration scored 90.5% against the original setup's 78.6%, at about one fifth the cost.在搜索从未见过的 14 张保留工单上,最终配置的得分为 90.5%,而原始配置为 78.6%,成本约为原来的五分之一。
Prompt caching, instructions, and effort are common levers for reducing cost. Our documentation covers even more. To run a holistic cost audit of application code that uses the Claude API, we've added /claude-api cost-optimize: it profiles where your spend goes, applies cost reductions, and, if you provide an evaluation, shows how savings trade off with performance.提示缓存、指令和努力程度是降低成本的常见杠杆。我们的文档还涵盖更多内容。为了对使用 Claude API 的应用程序代码进行整体成本审计,我们新增了 /claude-api cost-optimize:它会分析你的支出去向,应用成本削减方案,并且如果你提供评估,它还会展示节省与性能之间的权衡。
cost-optimize starts by finding where your tokens go: from your organization's usage and cost reports if you have a Claude Admin API key, from the usage object on each API response if your application logs it, or, failing both, by reading your request-building code and estimating.cost-optimize 首先找出你的 token 用在哪里:如果你有 Claude Admin API 密钥,则从组织的使用量和成本报告中获取;如果你的应用记录了每个 API 响应中的用量对象,则从那里获取;如果两者都没有,则通过阅读你的请求构建代码并进行估算。
It then ranks the available savings, starting with prompt caching, trimming what each request carries (including a prompt-audit), bounding output, and batching unattended work. If you supply an evaluation, it goes further and computes cost and performance across effort levels and model choices. 然后,它对可用的节省方案进行排序,从提示缓存开始,包括精简每个请求携带的内容(包括 prompt-audit)、限制输出长度,以及将无人值守的任务进行批处理。如果你提供评估,它会进一步计算不同努力程度和模型选择下的成本与性能。
We ran this on four public benchmarks, starting with Sonnet 5 as a baseline (Figure 7):我们在四个公共基准上运行了该工具,以 Sonnet 5 作为基线(图7):

Start with /claude-api prompt-audit when you've migrated to a frontier Claude model and want to check your existing prompts against it. It scans the prompts, skills, and tool descriptions in your working directory. This can be application code that calls the Claude API or Claude Code's configuration (CLAUDE.md, skills). It removes common anti-patterns that hobble frontier models.当你已迁移到前沿 Claude 模型并希望检查现有提示时,请从 /claude-api prompt-audit 开始。它会扫描工作目录中的提示、技能和工具描述。这些可以是调用 Claude API 的应用程序代码,也可以是 Claude Code 的配置(CLAUDE.md、skills)。它会移除阻碍前沿模型的常见反模式。
Reach for /claude-api cost-optimize when your application uses the Claude API and you want a cost audit. It profiles token spend and then tests different levers: it applies prompt-audit, but also checks for ways to lower cost via prompt caching, batching unattended work, or bounding output. If you provide an evaluation, it measures the effort and model selection trade-offs.当你的应用程序使用 Claude API 并希望进行成本审计时,请使用 /claude-api cost-optimize。它会分析 token 支出,然后测试不同的优化手段:它应用 prompt-audit,同时也会检查通过提示缓存、批处理无人值守工作或限制输出来降低成本的方法。如果你提供评估,它还会衡量努力程度和模型选择之间的权衡。
Finally, use /claude-api hillclimb for an iterative search over cost and performance. Given an evaluation, Claude splits it into train and test sets, then proposes updates to your application that aim to reduce cost while maintaining baseline performance. Claude reads the failing train cases to guide the search, and the final configuration is scored on the held-out test set.最后,使用 /claude-api hillclimb 进行成本与性能的迭代搜索。给定一个评估,Claude 会将其拆分为训练集和测试集,然后提出针对你的应用的更新建议,目标是在保持基线性能的同时降低成本。Claude 会阅读失败的训练案例来引导搜索,最终配置会在保留的测试集上进行评分。
To learn more: 了解更多:
Get the developer newsletter获取开发者通讯
Product updates, how-tos, community spotlights, and more. Delivered monthly to your inbox.产品更新、操作教程、社区亮点等内容,每月发送到你的邮箱。