Context Accumulation Strategies
As chains progress, context naturally grows. Managing this accumulation is crucial for chains that need information from multiple previous steps.
The Accumulation Problem
Each step may need information from earlier steps:
Step 1: Extract entities
Step 2: Analyze relationships (needs Step 1)
Step 3: Generate summary (needs Steps 1 and 2)
Step 4: Create report (needs Steps 1, 2, and 3)
Step 5: Review and refine (needs all previous)
Naive accumulation leads to context explosion.
Accumulation Patterns
Pattern 1: Selective Carry-Forward
Only carry forward what future steps need:
async function selectiveAccumulation(input) {
// Step 1
const step1Result = await extractEntities(input);
const step1Carry = {
entities: step1Result.entities, // Needed later
// Discard: raw_text, processing_metadata, confidence_scores
};
// Step 2
const step2Result = await analyzeRelationships(step1Carry);
const step2Carry = {
...step1Carry, // Keep Step 1 essentials
relationships: step2Result.relationships // Add Step 2 essentials
// Discard: analysis_details, intermediate_calculations
};
// Step 3 uses accumulated essentials only
const step3Result = await generateSummary(step2Carry);
return step3Result;
}
Pattern 2: Layered Context
Organize context into layers by importance:
Loading Prompt Playground...
Pattern 3: Checkpoint Summaries
Create summaries at checkpoints:
async function checkpointAccumulation(input) {
let context = { original: input };
// Phase 1: Steps 1-3
const step1 = await runStep1(context);
const step2 = await runStep2({ ...context, step1 });
const step3 = await runStep3({ ...context, step1, step2 });
// Checkpoint: Summarize Phase 1
const phase1Summary = await summarize({
step1, step2, step3
});
// Phase 2: Steps 4-6 (uses summary, not full details)
context = { original: input, phase1Summary };
const step4 = await runStep4(context);
// ... continue with compressed context
}
Building Effective Context Structures
Structured Context Object
const chainContext = {
// Original input (compressed if long)
original: {
summary: "User asked to analyze quarterly report",
key_requirements: ["trends", "recommendations"]
},
// Accumulated facts (not full outputs)
facts: {
entities: ["Company A", "Product X", "Q3 2024"],
sentiment: "positive",
topics: ["growth", "expansion"]
},
// Key findings from each phase
findings: {
phase1: "Revenue increased 15%",
phase2: "Customer satisfaction improved"
},
// Current state
current: {
step: 4,
objective: "Generate recommendations"
}
};
Progressive Summarization
Loading Prompt Playground...
Accumulation Strategies by Chain Type
Analysis Chains
const analysisAccumulation = {
// Keep: Key findings, scores, classifications
// Discard: Raw analysis details, confidence breakdowns
accumulate: (stepResult) => ({
finding: stepResult.summary,
score: stepResult.score,
classification: stepResult.category
})
};
Generation Chains
const generationAccumulation = {
// Keep: Constraints, style notes, generated content reference
// Discard: Draft iterations, rejected options
accumulate: (stepResult) => ({
contentType: stepResult.type,
constraints: stepResult.appliedConstraints,
qualityScore: stepResult.score
})
};
Research Chains
const researchAccumulation = {
// Keep: Sources, facts, citations
// Discard: Full source text, search results
accumulate: (stepResult) => ({
facts: stepResult.extractedFacts,
sources: stepResult.sourceIds,
confidence: stepResult.factConfidence
})
};
Handling Context Overflow
When context grows too large:
async function handleOverflow(context, maxTokens) {
const currentTokens = estimateTokens(context);
if (currentTokens <= maxTokens) {
return context;
}
// Strategy 1: Remove oldest items
if (context.history && context.history.length > 0) {
context.history = context.history.slice(-5); // Keep last 5
}
// Strategy 2: Summarize verbose fields
if (context.details && estimateTokens(context.details) > 500) {
context.details = await summarize(context.details, 200);
}
// Strategy 3: Remove non-essential fields
delete context.metadata;
delete context.debug;
return context;
}
Exercise: Design Accumulation Strategy
Design an accumulation strategy for this chain:
Loading Prompt Playground...
Key Takeaways
- Context naturally accumulates in chains
- Selective carry-forward keeps only essential data
- Layered context organizes by importance
- Checkpoint summaries compress at intervals
- Structure context objects for clarity
- Progressive summarization prevents overflow
- Different chain types need different strategies
- Always have an overflow handling strategy
Next, we'll explore summarization and compression techniques in detail.

