Module 11

Advanced Decomposition: Durability, Compaction, and Case Facts

This module covers the advanced logic needed to maintain performance and accuracy as long-running research sessions approach the context window limit, and to ensure that multi-agent systems recover intelligently from intermediate failures rather than failing the entire workflow.

Answer key Module11_Complete.ipynb

1. Context Management: Server-Side Compaction

As sessions approach the 1M-token context window limit, they can suffer from context rot, where the model loses focus on early instructions or misses information in the middle of long inputs, the "lost in the middle" effect.

Use the compact_20260112 strategy with a minimum trigger of 50,000 tokens. Claude automatically generates a concise summary of the conversation and replaces stale content with a compaction block.

Architect note: you must pass the resulting compaction block back to the API on all subsequent turns. The API then automatically drops all messages prior to it, resetting the context while preserving the narrative summary.

Long-running loops should not wait for a hard crash to manage context. Poll usage.context_window_remaining and trigger compaction proactively at 80% utilization.

2. The Case Facts Pattern

Compaction summaries can lose precise, non-negotiable details like confirmed budget figures, meeting dates, or transactional IDs.

Extract critical facts into a persistent case_facts block placed after the compaction block and outside the summary.

Compaction plus case facts
<compaction>
Summary of long-running research thread...
</compaction>

<case_facts>
- Max Budget: $50k
- Renewal Decision Date: 2026-09-15
- CRM Account ID: acct_7781
</case_facts>

Rule: case_facts is never summarized or compacted. It keeps hard data available even after the conversational narrative is compressed.

2a. Position-Aware Input Ordering

To mitigate the "lost in the middle" effect before compaction is needed, place key findings summaries at the beginning of aggregated inputs and organize detailed results with explicit section headers.

Aggregated input layout
<key_findings>
- Max Budget: $50k
- Current Competitor: ContosoAI
- Decision Date: 2026-09-15
</key_findings>

## Research Detail: Budget
...

## Research Detail: Competitor
...

Do not bury critical facts between long raw excerpts. Put the summary first, then the detailed evidence.

3. Decomposition: Fixed Pipelines vs. Adaptive Plans

Orchestrators must choose between predictable efficiency and dynamic durability.

  • Prompt chaining (fixed sequential): a hard-coded sequence such as research → draft → review. It is predictable and fast but brittle. If research surfaces a deal-breaker, the system may still attempt to draft a useless report.
  • Adaptive decomposition (dynamic): the coordinator inspects each subagent's output and re-plans the next steps based on findings. If research finds a prospect already uses a competitor, the coordinator skips the standard pitch and spawns a competitor-positioning subagent instead.

Adaptive systems cost more coordinator turns, but they are the durable pattern for open-ended work.

4. The Subagent Failure-Report Contract

A coordinator's ability to recover is bounded by the quality of the failure report it receives from a subagent.

Subagents should return a structured contract instead of generic prose errors.

JSON (coordinator-level failure report)
{
  "status": "failed",
  "failure_type": "TRANSIENT",
  "partial_results": [
    "Found pricing page",
    "Confirmed enterprise plan exists"
  ],
  "recommended_next_step": "Retry competitor review after rate limit clears."
}
  • failure_type: an enum such as TRANSIENT or VALIDATION, allowing the coordinator to branch without re-reading prose.
  • partial_results: lets the coordinator salvage what worked instead of wasting the entire run.
  • recommended_next_step: the subagent's local assessment of what it learned about the failure.

Outer-loop signal: isRetryable from Module 9 is a tool-level signal. This structured report is the coordinator-level signal for orchestrating complex recoveries.

5. Thinking Hygiene

On newer models, thinking blocks are kept in context by default and can consume the window quickly during long-horizon work.

To preserve context, use a lower compaction trigger or explicitly set "thinking_output": "omitted" on turns where narrative continuity is not required.

Lab Exercise: Managing a Long-Horizon Research Session

Self-driven lab Module11_Self_Driven_Lab.ipynb

Objective: master adaptive re-planning, compaction block persistence, and the case facts pattern.

  1. Iterative refinement: implement an adaptive coordinator that evaluates subagent outputs for gaps, missing coverage, or contradictions, then re-delegates targeted follow-up tasks until a quality threshold is met.
  2. Case facts extraction: create a case_facts block that persists Max Budget: $50k. Run a compaction trigger and verify the agent still knows the exact budget despite summary compression.
  3. Position-aware ordering: build an aggregated input where key_findings appear first and detailed research is grouped under explicit section headers.
  4. Intelligence recovery: simulate a research subagent failure. Ensure it returns a structured failure object with partial_results. Verify the coordinator uses partial data to finish the report rather than re-running the failed search.
  5. Compaction persistence: implement the compact_20260112 header. Verify that you pass the compaction block back in the subsequent messages.create call to keep the context window fresh.
  6. Effective context verification: after the compaction block fires, call client.messages.count_tokens on the next outgoing payload to verify the effective context size.
  7. Thinking hygiene: conduct a 10-turn research session with Adaptive Thinking enabled. Observe context accumulation and implement a trigger to omit thinking blocks for non-strategic summary turns.

Exam tip: compaction preserves narrative continuity; case_facts preserves exact facts; subagent failure reports preserve recovery options.