摘要:那就是:Claude Sonnet 4 现在支持 100 万 token 的上下文窗口 —— 这相当于在一次对话中处理 75 万个单词,或 7.5 万行代码。(1个token相当于0.75个单词,1行代码大概10个单词长度。)
进入8月后,Anthropic 推出了一个相当让开发者兴奋的更新!
那就是:Claude Sonnet 4 现在支持 100 万 token 的上下文窗口 —— 这相当于在一次对话中处理 75 万个单词,或 7.5 万行代码。(1个token相当于0.75个单词,1行代码大概10个单词长度。)
对一些开发者来说,这个概念可能有点抽象。我们可以这样理解:
一次性丢给Claude:一整本小说(大约 18 万 tokens)、一份配套的深入分析、研究资料,以及复杂的指令,完全不在话下,甚至还有很多的 token 空间等你开发。
这对于复杂、大规模的项目来说非常实用。
那究竟该怎么用这个新功能更新呢?小编帮各位整理了几种用法,希望能帮助到大家。
Google 的 Gemini 和 OpenAI 都有 100 万 token 的模型,所以看到 Anthropic 追上来是好事。但 Claude 的实现特别强调“有效上下文窗口”,也就是保证它能真正理解并利用你提供的大量信息。
和一些竞品在长上下文里出现注意力衰减不同,早期反馈显示 Claude 在扩展后的上下文范围内依然保持了稳定表现。
几个关键点:
完整项目理解你可以上传整个项目,Claude 会基于整体架构、代码风格和依赖关系,提供更契合的建议。长周期自主任务Claude 能够处理复杂的多步骤开发任务,保持上下文记忆,不会忘记之前的决策。更好的代码生成当你让 Claude 开发新功能时,它能看见整个应用结构,生成更合理、更集成的方案。全面代码审查上传整个代码库进行分析、重构建议或安全审计。如果你准备好利用这个扩展上下文窗口,可以按照以下步骤操作。
Node.js:
复制
npm install @anthropic-ai/sdk1.Python:
复制
pip install anthropic1.Node.js 示例:
复制
import Anthropic from '@anthropic-ai/sdk';const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY,});1.2.3.4.5.Python 示例:
复制
import anthropicclient = anthropic.Anthropic( api_key="your-api-key-here")1.2.3.4.复制
async function analyzeCodebase(files) { // Combine multiple files into a single prompt const combinedContent = files.map(file => `// File: ${file.path}\n${file.content}\n\n` ).join(''); const message = await anthropic.messages.create({ model: "claude-sonnet-4-20250514", max_tokens: 4000, messages: [{ role: "user", content: `Please analyze this entire codebase and provide:1. Architecture overview2. Potential improvements3. Security considerations4. Performance optimization opportunities1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.复制
Here's the codebase:${combinedContent}` }] }); return message.content[0].text;}1.2.3.4.5.6.7.def process_large_documentation(doc_content): message = client.messages.create( model="claude-sonnet-4-20250514", max_tokens=4000, messages=[{ "role": "user", "content": f"""Please create a comprehensive technical summary and implementation guide based on this extensive documentation:1.2.3.4.5.6.7.8.9.复制
{doc_content}Focus on:- Key implementation steps- Code examples- Best practices- Common pitfalls to avoid""" }] ) return message.content[0].text1.2.3.4.5.6.7.8.9.10.11.Web 应用可用 Express.js + Multer 处理多文件上传,并交给 Claude 分析。
示例代码:
复制
const multer = require('multer');const fs = require('fs').promises;1.2.复制
const upload = multer({ dest: 'uploads/' });app.post('/analyze-project', upload.array('files'), async (req, res) => { try { const files = ; for (const file of req.files) { const content = await fs.readFile(file.path, 'utf-8'); files.push({ path: file.originalname, content: content }); } const analysis = await analyzeCodebase(files); res.json({ analysis }); } catch (error) { res.status(500).json({ error: error.message }); }});1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.扩展上下文意味着价格调整:
输入超 20 万 tokens:每百万输入 $6 / 输出 $22.5输入少于 20 万 tokens:每百万输入 $3 / 输出 $15还可用简单的 Token 估算函数来预估成本。这里有个使用示例。
复制
function estimateTokenCount(text) { // Rough estimation: 1 token ≈ 4 characters for English text // For code, it's often closer to 1 token ≈ 3 characters return Math.ceil(text.length / 3.5);}1.2.3.4.5.复制
function estimateCost(inputTokens, outputTokens) { const inputCost = inputTokens > 200000 ? (inputTokens / 1000000) * 6 : (inputTokens / 1000000) * 3; const outputCost = outputTokens > 200000 ? (outputTokens / 1000000) * 22.50 : (outputTokens / 1000000) * 15; return inputCost + outputCost;}1.2.3.4.5.6.7.8.9.10.11.多文件重构按需求整体重构项目。
复制
async function refactorProject(files, requirements) { const prompt = `Refactor this entire project according to these requirements:${requirements}1.2.3.4.复制
Current codebase:${files.map(f => `// ${f.path}\n${f.content}`).join('\n\n')}Please provide the refactored files with explanations.`; // Process with Claude...}1.2.3.4.5.6.7.测试套件生成自动生成完整单测、集成测试和边界测试。复制
def generate_test_suite(codebase_files): combined_code = "\n\n".join([ f"# File: {file['path']}\n{file['content']}" for file in codebase_files ]) prompt = f"""Generate a comprehensive test suite for this entire codebase.Include unit tests, integration tests, and edge cases.1.2.3.4.5.6.7.8.9.复制
Codebase:{combined_code}""" # Send to Claude API...1.2.3.4.5.这次升级让 Anthropic 在上下文长度上超越了 GPT-5,也让 Claude 成为大规模复杂项目的理想选择。
虽然 Google Gemini 提供 200 万 tokens,Meta Llama 4 Scout 宣称 1000 万 tokens,但 Anthropic 的“有效上下文窗口”更强调 Claude 对内容的真实理解力。
Claude 的 100 万 token 上下文窗口为开发者带来了新的工作流可能性。如果你需要全局代码分析,或想打造更智能的开发工作流,这次升级可以说是重要的基础。
来源:51CTO一点号