Define Context Compression Rules for Long-Running AI Agents

Create a formal policy for compressing conversation history and managing agent memory to ensure long-running threads remain efficient without losing critical task data.

Agent prompt for Claude and ChatGPT

Prompt

Prompt
Act as a Lead AI Architect. Draft a set of rules for managing context windows in long-running agent threads. Focus on compression, memory compaction, and state recovery. Follow these rules: 1. Define specific triggers for summarization based on token usage thresholds. 2. Establish criteria for which messages must remain immutable vs. which can be summarized. 3. Include a protocol for handling failure recovery and state serialization. 4. Use clear, non-technical language that developers can implement directly. Output format: 1. Compression Triggers (list). 2. Message Retention Policy (table with columns: Message Type, Status, Retention Rule). 3. Memory Compaction Protocol (numbered list). 4. Failure Recovery Steps (numbered list). This is technical architectural guidance; review against your specific system constraints before implementation.

Example output

Memory compaction protocol for long-running agentsFour steps to compress older compressible messages
  1. Identify compressible messagesStep 1Compressible messages older than 10 turns
  2. Generate summaryStep 2Write a 3-sentence summary of the block
  3. Replace raw messagesStep 3Put the summary object in the active context
  4. Archive raw messagesStep 4Move to a secondary vector database for audit

Example for: a customer support orchestration layer managing 24/7 ticket resolution threads.

1. Compression Triggers
  • Token Count: Trigger summarization when the context window reaches 75% of the model capacity.
  • Turn Count: Trigger a state snapshot every 20 interaction turns regardless of token count.
  • Context Switch: Trigger a full memory dump if the agent switches from a diagnostic task to a resolution task.
2. Message Retention Policy
Message TypeStatusRetention Rule
System PromptsImmutableAlways keep in raw format
Critical DecisionsImmutablePin to long-term memory store
General Chit-ChatCompressibleSummarize every 5 turns
Technical LogsCompressibleReplace with status summary
3. Memory Compaction Protocol
  1. Identify messages marked as Compressible older than 10 turns.
  2. Generate a 3-sentence summary of the interaction block.
  3. Replace raw messages with the summary object in the active context.
  4. Move raw messages to a secondary vector database for audit logging.
4. Failure Recovery Steps
  1. Serialize current context and active tool states to a persistent JSON file.
  2. On system restart, load the latest state snapshot into the agent memory.
  3. Validate current task integrity against the last saved checkpoint.
  4. If integrity fails, prompt the user to confirm the last successful action.
Written by AI from this prompt. Your results will vary.

Tips and common edits

Why this works

  • Forces the model to categorize data types, preventing loss of vital logic.
  • Uses a structured table to clarify complex retention rules for developers.
  • Provides actionable, step-by-step protocols for both maintenance and failure recovery.

Use this when your AI agents are losing context or hitting token limits in long-running sessions. Paste this prompt into Claude, ChatGPT, or Gemini. No additional input is needed; the model will generate a standardized framework for your development team to apply to your agent orchestration layer.

Common edits

  • Adjust the token threshold based on the specific model context window (e.g., 128k vs 200k).
  • Add specific data privacy requirements if your agents handle PII or sensitive user information.

Reuse it: Use in a workflowAdapt in the builder

Prompt details

By
Prompts-GPT editors
Published
How prompts are written and checked

Did this prompt work?

Agent OrchestrationClaude

Design Idempotent API Tool Schemas and Retry Policies

Generate robust JSON tool schemas for AI agents that enforce idempotency, define safe retry status codes, and specify clear error recovery strategies for external APIs.

Agent OrchestrationClaude

Design Mock Tool Fixtures for Agent Trajectory Testing

Generate deterministic mock tool responses to test agent decision trees, edge-case failures, and schema-breaking payloads without live API usage or costs.

Agent OrchestrationClaude

Map AI Agent State Payloads and Handover Contracts

Generate a strict Pydantic state schema for agent pipelines that defines delta updates, discard rules, and structured rejection envelopes for handoffs.

Agent OrchestrationClaude

Triage Dead-Letter Queue Logs for Failed AI Agents

Sort unhandled agent run failures into an actionable classification table separating tool downtime, invalid schema calls, and missing user context.

Agent OrchestrationClaude Code

Parallel Git Worktree Task Split with Interface Contracts and Merge Order

Turns a pasted feature spec into independent coding-agent tasks with file ownership, interface contracts, a merge order and a done command for each task.

Agent Orchestration

Create Error Handling Contracts for AI Agents

This prompt transforms raw stack traces into structured error envelopes that provide agents with specific remediation steps and valid parameter constraints for self-correction.