← /blog
· ACE Engineering#llm #backend #cost #agent-trajectory-compaction #tokens #gateway #eval #scorecard #research #managed-api-stack #agent #vision

Halting Runaway Agent Loops & Context Growth: Agent Trajectory Compaction

An 800-example benchmark evaluating ACE's agent trajectory compactor: 38.5% net token savings, 91.7% information retention, and <0.3ms latency on multi-turn ReAct loops.

Halting Runaway Agent Loops & Context Growth: Agent Trajectory Compaction

As AI applications evolve from single-turn chat prompts into multi-turn autonomous agents—operating across frameworks like LangGraph, AutoGen, CrewAI, and custom ReAct loops—engineering teams unlock extraordinary autonomous capabilities. However, scaling these agents introduces a major operational challenge: exponential context window growth.

In a multi-turn agent loop, every new turn re-transmits the complete execution history: system instructions, user prompts, assistant tool call invocations, and verbose tool outputs (SQL query results, terminal logs, web HTML structures, and high-resolution vision screenshots). By turn 20, an agent session can re-transmit tens of thousands of tokens per step. A single stuck loop can burn thousands of dollars in minutes.

Today, we are excited to introduce agent_trajectory_compaction, ACE's high-performance gateway skill that folds multi-turn agent histories into dense, high-retention summaries while detecting and halting runaway tool execution loops.


How It Works: Sliding-Window State Compression

Unlike single-turn prompt compression (which prunes individual filler words inside prose prompts), agent_trajectory_compaction respects the exact multi-turn execution contract required by agentic frameworks:

[ Incoming Request (Messages Array across N turns) ]
                        │
                        ▼
       [ 1. Tool Loop Guardrail Check ] ── (Halts cyclic repeated tool loops)
                        │
                        ▼
       [ 2. Trajectory Structure Parser ]
         ├── Leading System Prompts    ──> Preserve Verbatim
         ├── Recent N Turns (Tail)     ──> Preserve Verbatim (with tool_calls & tool_call_id bindings)
         └── Historical Turns (Middle) ──> Multimodal Image Stripping + Extractive Status Summary
                        │
                        ▼
       [ 3. Compacted Output Construction ]
         [ System Prompts ] + [ [trajectory summary] Node ] + [ Recent Turns ]

Key Capabilities

  1. Protocol-Safe Structure Preservation: System instructions and recent N turns remain verbatim with exact tool_calls and tool_call_id bindings, ensuring downstream API schemas never break.
  2. Multimodal Vision Payload Stripping: Historical screenshots (image_url or image base64 blocks) from older turns are automatically stripped while preserving the text action description. This slashes 85–95% of vision token overhead on computer-use agents.
  3. Runaway Loop Guardrails: Computes rolling signature hashes of assistant tool calls (function_name + arguments). If an agent becomes trapped calling the same tool repeatedly with identical parameters, ACE immediately halts the cycle and reports a loop prevention event.
  4. Sub-Millisecond Execution Overhead (<0.3ms): Executes deterministically on the request path in <0.3ms without requiring external LLM model calls on the critical path.

Empirical Benchmark Results (n=800)

We evaluated agent_trajectory_compaction across an 800-sample benchmark dataset spanning four major agent benchmark domains (AgentBench, WebArena, ALFWorld, and SWE-bench):

Benchmark Family Domain Token Reduction (%) Must-Keep Retention (%)
WebArena Web Browsing & Navigation 44.55% 100.0%
AgentBench OS & Database Operations 37.94% 66.67%
SWE-bench Coding & Test Execution 35.40% 100.0%
ALFWorld Text Environment Control 32.05% 100.0%
OVERALL AVG Multi-Turn Agent Trajectories 38.47% 91.67%

Latency Profile

  • p50 latency: 0.169 ms
  • p95 latency: 0.196 ms
  • p99 latency: 0.276 ms

Financial Impact: 38.5% Net Cloud Savings

For an engineering team executing 100,000 multi-turn agent steps per day (averaging 4,000 input tokens per turn at $2.50 / 1M tokens):

  • Before Compaction: $1,000/day ($30,000/month)
  • After Compaction: $615.30/day ($18,459/month)
  • Net Financial Impact: $11,541 saved per month with zero application code modifications.

Unlocking Scalable Autonomous Workflows

By combining sub-millisecond trajectory folding with active loop guardrails, engineering teams can now deploy ambitious multi-turn autonomous agents with full confidence in cost predictability and system reliability.

Enable agent_trajectory_compaction in your ACE deployment environment:

export ACE_AGENT_COMPACTION=on
export ACE_AGENT_MAX_TURNS=15
export ACE_AGENT_KEEP_RECENT=3

Or toggle it per developer key via the ACE Fleet dashboard settings page.