datawhalechina/hello-agents · error · TimeoutException

LLM思考超时

Error message

LLM思考超时

What it means

TimeoutException from BaseAgent.think: asyncio.wait_for around self.llm.ainvoke(full_prompt) exceeded self.timeout seconds and cancelled the LangChain LLM call. LLM inference latency (especially long prompts producing long outputs) routinely exceeds default agent timeouts tuned for tools, so thinking is the most common place this fires.

Source

Thrown at Co-creation-projects/Apricity-InnocoreAI/agents/base.py:116

            if self.history:
                history_str = "\n".join(self.history[-10:])  # 只保留最近10条
                full_prompt += f"\n\n历史记录:\n{history_str}"
            
            # 调用 HelloAgent LLM
            response = await asyncio.wait_for(
                self.llm.ainvoke(full_prompt),
                timeout=self.timeout
            )
            
            response_text = response.content if hasattr(response, 'content') else str(response)
            
            self._add_to_history(f"LLM prompt: {prompt}")
            self._add_to_history(f"LLM response: {response_text}")
            
            return response_text
            
        except asyncio.TimeoutError:
            raise TimeoutException("LLM思考超时")
        except Exception as e:
            raise AgentException(f"LLM思考失败: {str(e)}")
    
    def _add_to_history(self, message: str):
        """添加到历史记录"""
        timestamp = datetime.now().isoformat()
        self.history.append(f"[{timestamp}] {message}")
        
        # 限制历史记录长度
        if len(self.history) > 100:
            self.history = self.history[-50:]
    
    def get_history(self, limit: int = 10) -> List[str]:
        """获取历史记录"""
        return self.history[-limit:]
    
    def clear_history(self):
        """清空历史记录"""

View on GitHub (pinned to 606a07d341)

Solutions

  1. Use a separate, larger LLM timeout (e.g. llm_timeout=180) in wait_for rather than reusing self.timeout.
  2. Trim the context passed to think() — summarize history instead of json.dumps of full records.
  3. Configure the provider client's own max_retries/timeout so provider-side throttling surfaces as its own typed error quickly.
  4. Enable streaming in ainvoke and accumulate chunks, resetting an idle timer per token instead of one total deadline.
  5. Retry think() once on timeout with a shortened prompt before failing the agent step.

Example fix

# before
response = await asyncio.wait_for(
    self.llm.ainvoke(full_prompt),
    timeout=self.timeout,
)

# after — dedicated, longer LLM deadline + single retry
async def _invoke():
    return await self.llm.ainvoke(full_prompt)
try:
    response = await asyncio.wait_for(_invoke(), timeout=self.llm_timeout)
except asyncio.TimeoutError:
    response = await asyncio.wait_for(_invoke(), timeout=self.llm_timeout)
    # second TimeoutError propagates as TimeoutException('LLM思考超时')
Defensive patterns

Strategy: retry

Validate before calling

# Sanity-check prompt size before invoking — huge prompts are the usual timeout cause
payload_chars = len(full_prompt)
if payload_chars > 50_000:
    full_prompt = full_prompt[:50_000]  # or summarize history

Try / catch

try:
    answer = await agent.think(prompt, context)
except TimeoutException:
    await asyncio.sleep(2)
    answer = await agent.think(shorten(prompt), context)  # one bounded retry

Prevention

When it happens

Trigger: Calling think() with a large prompt (full paper abstracts / JSON context appended) against a slow model endpoint (self-hosted vLLM under load, rate-limited OpenAI tier); streaming disabled so the full completion must finish within one deadline; network stall between client and inference provider.

Common situations: Default timeout (often 30-60s) too short for reasoning models or 4K-token outputs; provider 429 retries inside langchain silently extending wall time; context dict serialized with indent=2 bloating prompt size; shared timeout attribute used for both tools and LLM though their latency profiles differ.

Related errors


AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14). Data as JSON: /api/errors/6532324d3e3c2e1e. Report an issue: GitHub.