Why LLM Code Review Is Slow and Keeps Disconnecting
This article breaks down why LLM-based code review pipelines stall or drop connections, and how to fix it. Instead of blaming model intelligence, it frames the problem around four measurable engineering variables: reasoning depth, cache hit ratio, token efficiency, and connection stability. You will find practical techniques for context trimming, prompt layering, structured output contracts, resumable retries, and a fixed evaluation set with core metrics to verify that optimizations improve speed without sacrificing review quality.
In real engineering environments, code review tasks rarely fail because the model is not smart enough. They fail because long-context reasoning stretches indefinitely, caching cannot be reused, token consumption spirals out of control, and network jitter triggers repeated reconnects. Many teams have seen the same pattern: for the same review task, one approach returns a conclusion in minutes while another runs for a long time with no result and keeps dropping the connection. This article does not benchmark any specific product. Instead, it abstracts the problem into four quantifiable engineering variables: reasoning depth, cache hit ratio, token efficiency, and connection stability, and lays out a complete optimization methodology.
1. Framing the Problem: Four Measurable Variables Behind “Slow and Broken”
What users feel most directly is “slow” and “disconnected,” but engineering work requires breaking that down; otherwise the bottleneck cannot be located.
- Reasoning Depth: the length of the model’s internal reasoning before it produces a conclusion. A longer reasoning chain may improve accuracy on complex logic, but it linearly amplifies both time to first token and total latency.
- Cache Hit Ratio: whether identical or prefix-consistent context can be reused. A high hit ratio saves substantial repeated computation when reviewing the same repository or the same batch of files.
- Token Efficiency: the total input and output tokens consumed to complete an equivalent task. Low efficiency means the same context window fills up faster, triggering truncation or compression, which in turn degrades conclusion quality.
- Connection Stability: whether the streaming connection is interrupted during long tasks and whether it can recover automatically. The first three variables determine how long a task takes; the fourth determines whether the task can finish at all.
Writing these four variables into monitoring is the prerequisite for all subsequent optimization. At a minimum, collect the following metrics:
task_total_latency # end-to-end latencyfirst_token_latency # time to first tokenreasoning_token_count # tokens consumed during reasoningcache_hit_ratio # cache hit ratioreconnect_count # number of reconnectstruncated_context_flag # whether context truncation occurred
2. Root Cause 1: Long Context and Reasoning Amplification Cause Nonlinear Latency Growth
The input for code review is naturally large: multiple files, call chains, dependency definitions, and commit history. When all of this is pushed into the context at once, the model must run attention over an extremely long sequence, and latency rises nonlinearly with sequence length. If a deep reasoning mode is enabled at the same time, that adds a long reasoning pass on top of the long sequence, so the two costs multiply.
A more subtle problem is “context pollution.” Once irrelevant test data, logs, or generated code enter the window, the model’s attention is diluted, which actually requires longer reasoning to converge, creating a negative feedback loop where slower leads to messier and messier leads to slower.
Practical recommendations:
- Trim context by change scope, keeping only the files touched by the current diff and their direct dependencies;
- Apply structured summarization (function signatures, exported interfaces, key branches) to oversized files before passing them in;
- Make deep reasoning optional, enabling it only for cross-module logic, concurrency, and security review.
3. Root Cause 2: Cache Misses Mean Repeated Computation Is Paid Over and Over
A typical signal of a low cache hit ratio is that latency barely changes across requests for the same repository with the same prompt prefix. The causes usually fall into three categories:
- Unstable prefix: timestamps, random IDs, and request sequence numbers are concatenated at the very front of the system prompt, so the prefix hash changes every time;
- Context order drift: file traversal order depends on directory enumeration and is inconsistent across requests;
- Failed placement of dynamic content: putting volatile content before stable content breaks prefix reuse.
The fix is to layer the prompt into a “stable prefix plus volatile suffix”:
[stable layer] role definition, review guidelines, output format constraints, static repository notes[semi-stable layer] list of changed files for this review (sorted before concatenation)[volatile layer] specific diff, runtime logs, temporary annotations
Sorting the file list and removing randomized fields can significantly improve reuse probability across requests.
4. Root Cause 3: Low Token Efficiency Triggers Truncation and Rework
Low token efficiency sets off a chain reaction: the window fills up early, context is truncated, the model loses key evidence, output becomes vague, the user follows up with more questions, and tokens are consumed again. The key to breaking the loop is turning a single large request into layered small requests:
- Layer 1: determine only the risk level of the change and which files need focused review;
- Layer 2: perform fine-grained review on high-risk files;
- Layer 3: aggregate into a structured conclusion (issue list, severity, fix suggestions, verification method).
Pairing this with output constraints (limiting fields and maximum item counts) can noticeably compress output tokens. An illustrative output contract looks like this:
{"issues": [{"file": "path", "line": 0, "severity": "high|medium|low","reason": "brief description", "fix": "suggestion", "verify": "verification method"}],"summary": "no more than 200 words"}
5. Root Cause 4: Repeated Reconnects Make Long Tasks Never Finish
Even when the first three factors are healthy, network jitter can still break a long-lived streaming connection. If the client has no recovery strategy, the task can only start over, which is the direct source of “ran for a long time with no result.”
Engineering practice should cover three points:
- Checkpoint resume semantics: the server saves the results of completed stages and resumes from the missing stage after reconnecting instead of rerunning everything;
- Exponential backoff retries: avoid storm-style reconnects, and set a maximum retry count and overall time limit;
- Layered timeouts: configure idle connection timeout, per-stage timeout, and total task timeout separately, making it easier to tell whether the issue is network-related or caused by overly long reasoning.
retry: base=500ms, factor=2, max=3, jitter=0.2stage_timeout: 120sidle_timeout: 30sresume_from: last_completed_stage
6. Observability and Evaluation: Replace Gut Feel with Data
“Feels slow” must be turned into a comparable baseline. Build a small fixed evaluation set (a set of real changes), run the same cases after each optimization, and record four core metrics: end-to-end latency, cache hit ratio, token consumption, and reconnect count, while checking whether conclusion quality has degraded. Only when all four improve at the same time can an optimization be considered a real success. If latency drops but review conclusions clearly get worse, that is a false optimization that trades quality for speed.
7. Implementation Checklist
- Trim context by diff, and apply structured summarization to long files first;
- Layer the prompt, with the stable prefix first and volatile content last;
- Sort the file list and remove random fields to improve reuse;
- Make reasoning depth configurable, enabled only in complex scenarios;
- Route output through a structured contract that limits fields and length;
- Support stage-level resume on reconnect plus exponential backoff and layered timeouts;
- Build a fixed evaluation set and a dashboard for the four core metrics.
When building intelligent coding and code review capabilities on Baidu AI Cloud, you can use object storage to hold evaluation samples, a message queue for peak shaving, a container platform to host inference services, and logging plus monitoring and alerting for full-chain observability, turning “slow” and “disconnected” into engineering problems that can be located and regression-tested. As long as you keep reasoning depth, cache hit ratio, token efficiency, and connection stability under control, the code review pipeline can go from “never finishes” to “stable and deliverable.”
