How to Accelerate Multi-Turn AI Agents with SGLang RadixAttention on Linux
LLM Serving

How to Accelerate Multi-Turn AI Agents with SGLang RadixAttention on Linux

Key Takeaway: Retaining KV Cache Trees Across Tool Turns

Multi-turn AI coding agents repeat 80% to 95% of their prompt tokens on every turn, resending system prompts, tool schemas, and earlier execution transcripts. Traditional inference engines evict or recompute key-value (KV) caches between turns, causing Time-To-First-Token (TTFT) latency to degrade from 300ms on turn 1 to over 3,200ms on turn 15.

SGLang solves this by maintaining an LRU radix tree of KV cache blocks directly in GPU VRAM, an architecture introduced in the SGLang RadixAttention paper (arXiv:2312.07104). It matches shared token prefixes across multi-turn agent iterations, bypassing redundant prefill computation and serving subsequent agent turns at memory-bandwidth speeds.

+-----------------------------------------------------------------------+
|  RADIXATTENTION KV CACHE TREE IN GPU VRAM                             |
+-----------------------------------------------------------------------+
                                  [Root]
                                     |
              +----------------------+----------------------+
              |                                             |
    [System Prompt + MCP Schemas]               [User Instructions]
    (4,096 tokens, Cached 100%)                 (512 tokens, Cached)
              |
              +----------------------+
              |                      |
      [Turn 1 Tool Result]   [Turn 2 Fork / Retry]
      (Cached Prefix)        (Reuses 92% Parent KV)

Architectural Comparison: SGLang RadixAttention vs vLLM and Ollama

When serving Qwen2.5-Coder-32B-Instruct on a 24GB GPU with AWQ 4-bit quantization across a multi-turn debugging agent loop, KV cache retention dictates tail latency:

Architectural Capability Ollama vLLM (--enable-prefix-caching) SGLang (RadixAttention)
KV Cache Data Structure Linear ring buffer Block-level hash table (PagedAttention) LRU Radix Tree (RadixCache)
Multi-Turn Linear History Recomputes evicted turns Caches exact block hashes Caches variable-length token prefixes
Tree Branching / Tool Retries Full prefill recomputation Partial hit on matching block boundaries Native subtree sharing across branches
Prefix-Aware Scheduling FIFO queue FCFS with block reuse Longest Prefix Match (--schedule-policy lpm)

While standard vLLM automatic prefix caching hashes fixed-size token blocks from left to right, SGLang’s radix tree handles tree branching natively. When an agent retries a failed bash command or branches into parallel sub-agents, SGLang reuses the common ancestor blocks without cache misses.


Step 1: Install SGLang with FlashInfer on Linux

Install SGLang with CUDA 12.4 support and FlashInfer kernels:

pip install --upgrade pip
pip install "sglang[all]>=0.4.3" --find-links https://flashinfer.ai/whl/cu124/torch2.4/flashinfer/

Verify GPU kernel compatibility:

python3 -c "import sglang; print('SGLang version:', sglang.__version__)"

Step 2: Launch SGLang with Production Agent Flags

Run the SGLang server using the OpenAI-compatible HTTP endpoint. We set --mem-fraction-static 0.88 to allocate maximum VRAM to the RadixAttention cache pool while reserving headroom for CUDA graphs:

python3 -m sglang.launch_server \
  --model-path Qwen/Qwen2.5-Coder-32B-Instruct-AWQ \
  --port 30000 \
  --host 0.0.0.0 \
  --mem-fraction-static 0.88 \
  --context-length 32768 \
  --schedule-policy lpm \
  --tp 1

Critical Flags Explained:

  • --schedule-policy lpm: Longest Prefix Match scheduling prioritizes incoming requests that share the largest existing KV cache subtree in VRAM.
  • --mem-fraction-static 0.88: Reserves 88% of free VRAM for model weights and the RadixAttention pool, preventing out-of-memory errors during long context extensions.
  • --context-length 32768: Enforces a 32k window suitable for deep code analysis.

Step 3: Connect Cursor and Claude Code to the Endpoint

SGLang exposes a drop-in OpenAI-compatible /v1/chat/completions endpoint on port 30000.

For Custom Python Agent Loops:

from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:30000/v1",
    api_key="EMPTY"
)

response = client.chat.completions.create(
    model="Qwen/Qwen2.5-Coder-32B-Instruct-AWQ",
    messages=[
        {"role": "system", "content": "You are an autonomous Linux DevOps agent."},
        {"role": "user", "content": "Inspect docker container exit logs."}
    ],
    temperature=0.2
)

print(response.choices[0].message.content)

For Cursor Configuration:

  1. Open Cursor Settings -> Models -> OpenAI API Key.
  2. Override the Base URL: http://localhost:30000/v1.
  3. Add model name: Qwen/Qwen2.5-Coder-32B-Instruct-AWQ.

Production Tip: Monitoring Radix Cache Hit Rates

Query the SGLang metrics endpoint to verify cache reuse during multi-turn runs:

curl http://localhost:30000/get_server_info

Look for tree_cache_hit_rate in the JSON response. If your agent maintains deterministic tool ordering and static system prompts, the hit rate will consistently measure above 90% across multi-turn sessions.

BC

Written by B. Cho

Systems & AI Infrastructure Engineer

Specializing in GPU inference engines (vLLM, SGLang, Ollama), CUDA memory allocation, and Model Context Protocol (MCP) infrastructure on Linux workstations.