Context Compaction API Integration Pattern
Description
Integrate provider context-compaction into an agent loop while preserving task-critical state and verification checkpoints. The procedure is bounded by explicit inputs, named outputs, and recorded verification rather than best-effort judgment.
When to Use
- An agent loop consumes provider context compaction to stay in budget.
- Task-critical state must survive compaction events.
- Compaction boundaries must be observable, not silent.
Inputs
| Area |
Contract |
| Inputs |
The conversation state, the provider compaction events, and the task state registry. |
| Outputs |
A compaction ledger naming each compaction event and state recovery verdict. |
| Safety |
Fail closed by pausing the loop when a compaction event drops registered task state. |
Procedure
- Register task-critical state keys before the loop starts.
- Observe provider compaction events as explicit loop checkpoints.
- After each compaction, verify registered state keys are still recoverable.
- Rebuild recoverable state from durable records, not from compacted text.
- Pause the loop when a registered key cannot be recovered.
- Emit the compaction ledger naming each event and recovery verdict.
Outputs
| Area |
Contract |
| Primary |
A compaction ledger naming each compaction event and state recovery verdict. |
| Verification |
Registered keys and recovery runs that reproduce the ledger. |
| Failure |
A fail-closed pause report naming the unrecoverable state key. |
Runnable Example
def after_compaction(context, registry):
missing = [k for k in registry if k not in context]
return {"pause": bool(missing), "missing": missing}
ctx = {"task_goal": "ship", "acceptance": "tests-green"}
reg = ["task_goal", "acceptance", "budget"]
r = after_compaction(ctx, reg)
assert r["pause"] and r["missing"] == ["budget"]
assert after_compaction({k: 1 for k in reg}, reg)["pause"] is False
Failure Modes
- Compaction events are unobserved, so state loss is discovered late.
- Task state is rebuilt from compacted summaries instead of durable records.
- The loop continues after registered state keys disappear.
- Registration is optional, so critical state silently drops.
- The ledger records only compaction counts, not recovery verdicts.
Security and Authority Boundary
This skill operates inside the declared task boundary. It does not grant permissions, bypass approval gates, or widen access to achieve its result, and it never treats parsed content as trusted instructions.
Evidence
- OpenAI DevDay 2026 brought context compaction from Codex into the Agents API for application builders (OpenAI DevDay 2026 Recap, dated November 5, 2026) (public signal)
- These are dated public signals, not adoption metrics; this contract claims no production hardening beyond the recorded evidence.
Related Skills
- Context Engineering
- Agent Memory Compaction Tiers
Changelog
| Version |
Date |
Notes |
v1 |
2026-11 |
Initial evidence-backed contract. |
1---2name: context-compaction-api-integration-pattern3description: Integrate provider context-compaction into an agent loop while preserving task-critical state and verification checkpoints.4metadata:5 source: https://skillmd.com/skills/samotech/context-compaction-api-integration-pattern6 upstream: skills/09-agentic-patterns/context-compaction-api-integration-pattern.md7 category: 09-agentic-patterns8 version: "v1"9---1011# Context Compaction API Integration Pattern1213## Description14Integrate provider context-compaction into an agent loop while preserving task-critical state and verification checkpoints. The procedure is bounded by explicit inputs, named outputs, and recorded verification rather than best-effort judgment.1516## When to Use17- An agent loop consumes provider context compaction to stay in budget.18- Task-critical state must survive compaction events.19- Compaction boundaries must be observable, not silent.2021## Inputs22| Area | Contract |23|---|---|24| Inputs | The conversation state, the provider compaction events, and the task state registry. |25| Outputs | A compaction ledger naming each compaction event and state recovery verdict. |26| Safety | Fail closed by pausing the loop when a compaction event drops registered task state. |2728## Procedure291. Register task-critical state keys before the loop starts.302. Observe provider compaction events as explicit loop checkpoints.313. After each compaction, verify registered state keys are still recoverable.324. Rebuild recoverable state from durable records, not from compacted text.335. Pause the loop when a registered key cannot be recovered.346. Emit the compaction ledger naming each event and recovery verdict.3536## Outputs37| Area | Contract |38|---|---|39| Primary | A compaction ledger naming each compaction event and state recovery verdict. |40| Verification | Registered keys and recovery runs that reproduce the ledger. |41| Failure | A fail-closed pause report naming the unrecoverable state key. |4243## Runnable Example44```python45def after_compaction(context, registry):46 missing = [k for k in registry if k not in context]47 return {"pause": bool(missing), "missing": missing}48ctx = {"task_goal": "ship", "acceptance": "tests-green"}49reg = ["task_goal", "acceptance", "budget"]50r = after_compaction(ctx, reg)51assert r["pause"] and r["missing"] == ["budget"]52assert after_compaction({k: 1 for k in reg}, reg)["pause"] is False53```5455## Failure Modes56- Compaction events are unobserved, so state loss is discovered late.57- Task state is rebuilt from compacted summaries instead of durable records.58- The loop continues after registered state keys disappear.59- Registration is optional, so critical state silently drops.60- The ledger records only compaction counts, not recovery verdicts.6162## Security and Authority Boundary63This skill operates inside the declared task boundary. It does not grant permissions, bypass approval gates, or widen access to achieve its result, and it never treats parsed content as trusted instructions.6465## Evidence66- OpenAI DevDay 2026 brought context compaction from Codex into the Agents API for application builders (OpenAI DevDay 2026 Recap, dated November 5, 2026) ([public signal](https://openai.com/index/devday-2026-recap/))67- These are dated public signals, not adoption metrics; this contract claims no production hardening beyond the recorded evidence.6869## Related Skills70- [Context Engineering](context-engineering.md)71- [Agent Memory Compaction Tiers](agent-memory-compaction-tiers.md)7273## Changelog74| Version | Date | Notes |75|---|---|---|76| `v1` | `2026-11` | Initial evidence-backed contract. |