बोला गया सारांश — साथ पढ़ने के लिए प्ले दबाएँ: बोली जा रही पंक्ति ऊपर रहती है।
हर request जो आप Claude को भेजते हैं उसके पास एक context window होता है। एक मिलियन tokens बहुत कुछ लगता है, लेकिन जब आप एक real agent ship कर रहे हों तो यह तेजी से खत्म हो जाता है। यहीं context management काम आती है: यह है कि आप window के अंदर कैसे रहें बिना महत्वपूर्ण चीजों को खोए।
Context क्या है
Context है सब कुछ जो Claude एक दिए गए turn पर देखता है :
- System prompt
- Message history
- Tool definitions और tool results
- Attached files और skills
- Thinking blocks
Context के पाँच components का diagram: system prompt, message history, tools, files और skills, और thinking blocks
यह हर एक API call का input है। आप इसके लिए input पर भुगतान करते हैं, और output पर भी भुगतान करते हैं। और एक बार window भर जाए, तो request fail हो जाता है।
तो लक्ष्य सब कुछ fit करना नहीं है। लक्ष्य है सही चीजें fit करना।
Anthropic long-running agents में context manage करने के लिए चार patterns प्रकाशित करता है। तीन first-class API features हैं, और एक design pattern है।
Context manage करने के चार patterns का diagram: just-in-time context, compaction, caching, और memory
Pattern 1: Just-in-time context
सब कुछ upfront load न करें। Agent को अभी जो चाहिए वह load करें, और इसे tools के माध्यम से अधिक pull करने दें जब यह माँगे।
एक compliance review agent के बारे में सोचें। इसे पूरी building code book अपने system prompt में नहीं मिलती — यह एक lookup_building_code tool को call करता है जब इसे एक specific section चाहिए। यह चारों में से design pattern है: API में कुछ खास नहीं, बस एक deliberate choice कि आप क्या load करते हैं और कब।
Pattern 2: Server-side compaction
जब एक conversation लंबा चलता है, Anthropic का server-side compaction पुराने turns को एक single block में summarize करता है। आप अपने request में एक context_management key जोड़कर opt in करते हैं, एक edit hold करते हुए एक type के साथ:
response = client. messages. create( model="claude-sonnet-4-5", max_tokens=1024, context_management={ "edits": [ {"type": "compact"} ] }, messages=messages, )
API auto-summarize करता है जब input trigger threshold को cross करता है। आपको conversation length को track करना नहीं है।
Pattern 3: Prompt caching
Prompt caching आपको request के stable parts को mark करने देता है — system prompt, tool definitions, एक long document — और उन्हें calls के across cost के एक fraction पर reuse करता है।
गणित इससे अधिक महत्वपूर्ण दिखती है। अगर आपका system prompt 4,000 tokens है और आप इसे एक घंटे में 100 बार call करते हैं, तो caching एक usable bill और finance से एक phone call के बीच का अंतर है।
Pattern 4: Memory tool
कुछ context को sessions के across survive करना चाहिए: user preferences, agent के running notes, क्या last week decide किया गया था। इसके लिए recommended primitive है memory tool।
यह कैसे काम करता है:
- Claude tool calls के माध्यम से एक memory directory को read और write करता है।
- आप storage backend को client-side implement करते हैं — एक file system, एक database, एक encrypted store, जो भी आप चाहते हैं।
- Anthropic auto-inject करता है एक system instruction जो Claude को work शुरू करने से पहले memory directory को check करने के लिए कहता है।
Browser में देखा गया एक memory directory, incidents और saas-pricing के लिए folders के साथ और एक previous session से एक saved incident note
Patterns को layer करना
एक production app में, आप आमतौर पर सभी चारों को एक साथ layer करेंगे। Compliance review agent अपने system prompt और tool definitions को cache करता है, और building code sections को just in time के माध्यम से lookup_building_code के via pull करता है।
हर pattern एक अलग failure mode को handle करता है: cost, window size, statelessness। जो आपके लिए break हो रहा है उससे match करने वाले चुनें।
Recap
- Context सब कुछ है जो Claude एक turn पर देखता है — और यह free या infinite नहीं है। एक बार window भर जाए, request fail हो जाता है।
- Just-in-time context : अभी जो चाहिए वह load करें, tools को बाकी pull करने दें। यह चारों में से design pattern है।
- Server-side compaction : एक context_management key जोड़ें, और API automatically पुराने turns को summarize करता है जब input trigger threshold को cross करता है।
- Prompt caching : request के stable parts को mark करें और उन्हें calls के across cost के एक fraction पर reuse करें।
- Memory tool : Claude tool calls के माध्यम से एक memory directory को read और write करता है; आप storage backend को own करते हैं, तो context sessions के across survive करता है।
- चार patterns, एक लक्ष्य। उन्हें hand से wire करें, या Claude managed agents का use करें, जो caching और compaction के साथ by default ship करते हैं।