Overview
Models are now performing long horizon tasks that regularly hit 1m+ tokens. As the conversation prefix grows, cache read costs grow quadratically and eventually outweighs all other token costs. Most information accumulated in agent traces doesn’t need to be kept forever, and clearing it with regular compactions converts the cost of a trace from quadratic to linear. If you perform the compaction when the user is inactive for long enough to miss cache, you can also reduce the severity of cache write costs, as shown below.Compacting on cache miss nearly halves the cost of a 50-turn trace. For best results, we recommend keeping the conversation as close to 128k tokens as possible.
For summary-based compactions that take 1-2 minutes, this strategy disrupts the user flow. By using Relace Compact, you get imperceptible compactions that finish within a couple of seconds.Prerequisites
Prepare Your Agent Trace
Construct
messages exactly as your agent holds them for inference: OpenAI v1/chat/completions, OpenAI v1/responses, or Anthropic v1/messages. The format is detected automatically by the API and returned in the same format.Call the Compact Endpoint
target_tokens sets the token budget for the retained context (defaults to 128k), and the optional agent_model tells us which model generated the trace so we can apply model-specific improvements and count tokens more accurately.