跪拜 Guibai
← Back to the summary

Five-Stage Agent Resuscitation Manual: Keeping Your AI Alive When Concurrency Spikes

When you run an Agent locally, you think it's Zhuge Liang: it knows everything, calls tools with ease, and even writes your weekly report.

Once you push it to production, as soon as concurrency rises a bit, it turns into an "artificial idiot": the page keeps spinning forever, the backend logs turn red, 504 Gateway Timeout, ReadTimeout, and 429 Too Many Requests are having a party, and finally the service just dies.

An Agent is an extremely fragile "chain of traps" system: it not only counts tokens and queries vector stores, but also fires HTTP requests to call tools, then makes the LLM chat. If any single link in the chain fails, the whole thing collapses like dominoes.

To pull your Agent out of the ICU, first understand how it kills itself across these 5 stages.

Full-Link Accident Map

Every time a user sends a message, the Agent's internal journey looks like this:

[Frontend user eagerly asks]
       │
       ▼
【Stage 1: Prompt Assembly】(dig through chat history, check token count)
       │
       ▼
【Stage 2: RAG Retrieval】(convert text to vectors, search document store, rerank)
       │
       ▼
【Stage 3: LLM First Reasoning】(LLM is stuck: should I answer directly or call a tool?)
       │
       ▼
【Stage 4: External Tool Execution】(query business DB, fetch web pages, call third-party APIs)
       │
       ▼
【Stage 5: LLM Final Output】(compress tool output into an answer, deliver to user)

Stage 1: Prompt Assembly - Tired Out Before Leaving Home

Many think splicing system prompt and user input with f"{sys_prompt} + {user_input}" takes only 0.1ms. But in production, many turn this step into a CPU meltdown scene.

1. How It Fails

2. Rescue Guide

Stage 2: Knowledge Recall (RAG) - The Vector Store Collapses While Searching

RAG is the Agent's "external brain", but under high concurrency that brain easily suffers a stroke.

1. How It Fails

2. Rescue Guide

Stage 3: LLM First Reasoning - Noisy Neighbors in the Shared Pool

Here, the LLM decides what to do. The pitfalls here are often not your bad code, but that you don't know how the vendor is screwing you.

1. How It Fails

2. Rescue Guide

Stage 4: External Tool Execution - Crew Drops, Captain Dies Too

After the LLM thinks, it commands the Agent to call real-world tools. This is often the worst accident area.

1. How It Fails

2. Rescue Guide

Stage 5: LLM Final Output - Gateway Waits 60s, Cries and Returns 504

Tools return data, the LLM prepares its final answer. Don't trip at the finish line.

1. How It Fails

2. Rescue Guide

Ultimate Lifesaver: Too Slow? Switch to Async Queueing!

If your Agent is stuck with heavy tasks like deep research, writing code, or long-form writing, and single execution will definitely exceed 30 seconds, stop trying to force it with regular HTTP requests.

Immediately switch to async task flow pattern: User clicks submit -> Backend pushes to a message queue (like Redis Stream or BullMQ) -> Instantly returns a Task ID to frontend -> Background distributed worker cluster processes slowly -> Frontend subscribes via SSE with the Task ID for typewriter effects.

This way, even if 10,000 people flood in concurrently, everyone queues nicely, and no one gets a 504 timeout package.

Comments

Top 1 of 2 from juejin.cn, machine-translated. The original thread is authoritative.

星云PLUS团队

Beyond these five stages, here's a pitfall about tool execution: The hardest part of a timeout isn't killing the request, it's not knowing whether the other side actually succeeded. For read APIs you can safely retry, but for write APIs (creating orders, placing orders, sending messages) you must carry an idempotency key or later query the receipt; otherwise one retry after a 504 means duplicate submission. Also, allocate the timeout budget per stage rather than as one global value: give assembly and RAG fixed caps, let tool execution keep margin based on the interface's P99, and always reserve budget for the output stage, or the first four steps could be healthy and the reply still gets a 504. One more low-cost trick: add structured metrics to each stage (stage name + error code + whether retried). When it's unresponsive you can tell at a glance whether it's a 429 or the connection pool refusing to lend, which is far more efficient for troubleshooting than adding just one global trace.

去伪存真

Thanks for the god-tier addition, especially the write-API timeout: the scariest thing isn't an error, it's the downstream actually succeeding while the gateway plays deaf and dumb; blindly retrying just gives the user a 'buy one get one (mandatory)'. And the 'grand-finale budget' reserved for output really is 'walking ninety-nine steps and tripping on the last one right in front of the user'.