datawhalechina/hello-agents · error · TimeoutException

LLM思考超时

Error message

LLM思考超时

What it means

BaseAgent.think wraps self.llm.ainvoke(full_prompt) in asyncio.wait_for(..., timeout=self.timeout); asyncio.TimeoutError is re-raised as a custom TimeoutException('LLM思考超时'). It means the LLM call did not complete within the agent's configured timeout (default set on the agent, often 60s), not that the model returned an error.

Source

Thrown at Co-creation-projects/Shawnxyxy-HealthRecordAgent/backend/agents/base.py:143

            self.trace("LLM TTHINKING TIME",
                {
                    "duration_sec": duration,
                    "prompt_tokens": len(full_prompt),
                }
            )
            
            response_text = response.content if hasattr(response, 'content') else str(response)
            
            self.trace("LLM RESPONSE", response_text)

            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(f"LLM思考超时")
        except Exception as e:
            raise AgentException(f"LLM思考失败: {str(e)}")
    # ========== Tool 机制 ==========
    def add_tool(self, tool_name: str, tool_func: Callable, description: str = ""):
        """添加工具"""
        self.tools[tool_name] = {
            "function": tool_func,
            "description": description
        }
    
    def get_tools_description(self) -> str:
        """获取工具描述"""
        if not self.tools:
            return "暂无可用工具"
        
        descriptions = []
        for name, tool_info in self.tools.items():
            descriptions.append(f"- {name}: {tool_info['description']}")

View on GitHub (pinned to 606a07d341)

Solutions

  1. Raise the agent's timeout (constructor/config) to comfortably exceed worst-case generation time.
  2. Shrink the prompt: trim history, summarize context, or cap tool output fed back into think().
  3. Catch TimeoutException at the call site and retry once — transient provider slowness often clears.
  4. If it persists, check provider status/latency and whether the base_url is reachable quickly.

Example fix

# before
agent = SymptomCheckAgent(timeout=30)  # too tight for long prompts

# after
agent = SymptomCheckAgent(timeout=180)
try:
    out = await agent.think(prompt)
except TimeoutException:
    out = await agent.think(shortened_prompt)  # retry with trimmed context
Defensive patterns

Strategy: retry

Try / catch

try:
    out = await agent.think(prompt)
except TimeoutException:
    await asyncio.sleep(2)
    out = await agent.think(trim(prompt))  # retry once with a shorter prompt

Prevention

When it happens

Trigger: Long prompts or slow reasoning models exceeding self.timeout; streaming disabled so the whole completion must land inside the window; provider under heavy load; the event loop being blocked elsewhere so the coroutine never progresses.

Common situations: Large health-record contexts producing multi-thousand-token prompts; tight timeouts copied from quick smoke tests; degraded LLM provider latency at peak hours.

Related errors


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