通过精准投喂代码片段而非完整文件来削减 token 浪费,单次调试会话可节省约 26 万 token,成本降幅显著。
大多数降低 AI API 成本的建议都聚焦于切换到更便宜的模型。这确实有效,但代价是用质量换取节省。以下技巧可以在不改变模型选择的情况下降低成本——通过消除那些从未产生实际价值的 token 浪费。
在用 TokenPulse 追踪了 30 天的 AI 使用情况后,我发现了持续浪费 30-50% token 的模式。以下是消除它们的方法。
最大的单一 token 浪费来源是:只需要部分内容时却粘贴了整个文件。
昂贵的模式:
User: Here's my entire auth module (500 lines).
Why is the token validation failing?
// 500 lines = ~30,000 tokens
// Re-sent on every subsequent message in the conversation
高效的模式:
User: Here's the validateToken function (20 lines)
and the middleware calling it (10 lines).
Why is validation failing?
// 30 lines = ~1,200 tokens
// 96% reduction for equivalent answer quality
对于一个 10 条消息的调试会话,高效模式可节省约 260,000 个上下文 overhead token——在 Claude Sonnet 上约合 $0.78,或在 GPT-4o 上约合 $0.65。
规则:粘贴特定函数加上周围 10-15 行上下文。如果模型需要更多,它会主动询问。
会话中的每条消息都会重新发送整个历史记录。一个 20 条消息的会话,假设每次交换平均 500 token,就会累积 10,000 个 token 的历史记录——每条新消息都会重新发送。
第 20 条消息的输入成本:
History tokens: 20 exchanges × 500 tokens = 10,000 tokens
New message: 200 tokens
Total input: 10,200 tokens per message
对比用 200 词的摘要开启新会话:
Summary: ~150 tokens
New message: 200 tokens
Total input: 350 tokens per message
第 20 条消息的输入 token 减少了 96%。
摘要然后重启的模式:
在 8-10 次交换后:
You: Summarize the key decisions, constraints, and
current state of this implementation in under 150 words.
[Start new conversation]
You: Context: [paste summary]
Continue from here: [your next question]
坚持执行这一模式,长期会话的每会话成本会大幅下降。
Anthropic 的 API 支持对超过 1,024 token 的系统提示词进行缓存。缓存后的 token 在后续请求中仅收取正常输入价格的 10%。
如果你使用 Claude API 且有较长的系统提示词——项目上下文、编码规范、架构文档——请缓存它:
const response = await anthropic.messages.create({
model: 'claude-sonnet-4-5',
max_tokens: 1024,
system: [
{
type: 'text',
text: longSystemPrompt, // Your project context
cache_control: { type: 'ephemeral' }
}
],
messages: [{ role: 'user', content: userMessage }]
})
对于一个 5,000 token 的系统提示词发送 100 次:
5,000 tokens × 100 requests × $3.00/1M = $1.50
启用缓存(首次请求全价,后续按 10% 计费):
5,000 tokens × $3.00/1M = $0.015 (首次)
5,000 tokens × 99 × $0.30/1M = $0.149 (缓存)
总计: $0.164 — 降低 89%
模糊的问题产生冗长、留有余地的回答。具体的问题产生简洁、有针对性的回答。简洁的回答成本更低。
Explain authentication in web applications.
产出:800-1,500 token 的回答,覆盖认证的方方面面
In my Express app using JWT, should I store
the refresh token in httpOnly cookie or localStorage?
One paragraph.
产出:150-250 token 的回答,准确回答你的需求
在提示词中加入"一段话"、"用两句话"或"只用要点"等限定词,可持续将输出 token 消耗降低 40-70%,同时不会丢失有用信息。
每次 API 调用都有最低成本——请求的开销、模型的上下文加载和响应结构。分别做五次一问一答的 API 调用,比一次调用问五个问题的成本更高。
Call 1: "What does this function do?" → $0.001
Call 2: "What are edge cases?" → $0.001
Call 3: "How would you test this?" → $0.001
Call 4: "Any performance concerns?" → $0.001
Call 5: "Suggest a better variable name?" → $0.001
Total: $0.005
"For this function: (1) what does it do,
(2) edge cases, (3) how to test,
(4) performance concerns, (5) better variable name?"
→ $0.002
批量处理减少了开销,同时让模型能够同时看到所有问题的上下文——往往还能得到更好的答案。
常见模式:用便宜模型准备和过滤上下文,然后只向贵价模型发送相关上下文。
// Step 1: Use cheap model to identify relevant sections
const relevantSections = await anthropic.messages.create({
model: 'claude-haiku-4-5', // $0.80/1M input
max_tokens: 500,
messages: [{
role: 'user',
content: `Given this 500-line file, identify only the
functions relevant to JWT authentication.
Return just the function names.
File: ${entireFile}`
}]
})
// Step 2: Extract only relevant code
const relevantCode = extractFunctions(
entireFile,
relevantSections.content[0].text
)
// Step 3: Send targeted context to expensive model
const analysis = await anthropic.messages.create({
model: 'claude-sonnet-4-5', // $3.00/1M input
max_tokens: 2048,
messages: [{
role: 'user',
content: `Analyze this JWT authentication code: ${relevantCode}`
}]
})
Haiku 调用的成本约为 $0.0003,用于处理整个文件并识别相关部分。Sonnet 调用处理的是减少 90% 的代码量。在大文件上的净节省:60-80%。
长会话中常见的一种模式是:重复解释模型已有的上下文:
Message 15: "As I mentioned earlier, this is a
Next.js app using TypeScript and Prisma..."
模型已有这些信息——你在第 1 条消息中就告诉过它。每次这样重复陈述都是在浪费 token。只有在真正丢失会话历史轨迹时才重新说明。
如果不追踪,这些优化是不可见的。实施全部七种技巧后,我的每月 AI 成本估算从 157 个会话的 $0.439 降至同体量下的 $0.26-0.28——降低了 37-41%。
单一影响最大的是精准上下文粘贴(技巧 1),仅这一项就贡献了约 25% 的总节省。
TokenPulse 可以让你轻松实时看到这些改变的成本影响——每个会话的成本会在聊天时更新,这样你可以立即看到新方法是否在节省 token。
免费使用,无需 API key,支持 Claude、ChatGPT、Gemini、DeepSeek 和 Grok。