ZhuLinsen/daily_stock_analysis · error · HTTPException

Agent mode is not enabled

Error message

Agent mode is not enabled

What it means

POST /api/v1/agent/research rejects the request with HTTP 400 'Agent mode is not enabled' when config.is_agent_available() returns False. Per src/config.py:2970 the availability gate combines the AGENT_MODE env var with whether an agent-safe LLM route is configured: AGENT_MODE=false always disables it, and when unset or true, at least one agent-safe route must exist. This is a pre-flight capability check, not a runtime failure of the ResearchAgent itself.

Source

Thrown at api/v1/endpoints/agent.py:422

    stock_code: Optional[str] = None

class ResearchResponse(BaseModel):
    success: bool
    content: str
    sources: List[str] = Field(default_factory=list)
    token_usage: int = 0
    error: Optional[str] = None


@router.post("/research", response_model=ResearchResponse)
async def agent_research(request: ResearchRequest):
    """Run a deep-research query via the ResearchAgent.

    Similar to the ``/research`` bot command but exposed as a REST endpoint.
    """
    config = get_config()
    if not config.is_agent_available():
        raise HTTPException(status_code=400, detail="Agent mode is not enabled")

    question = request.question
    context: Optional[Dict[str, Any]] = None
    if request.stock_code:
        question = f"[Stock: {request.stock_code}] {question}"
        context = {"stock_code": request.stock_code}

    try:
        from src.agent.research import ResearchAgent
        from src.agent.factory import get_tool_registry
        from src.agent.llm_adapter import LLMToolAdapter

        registry = get_tool_registry()
        llm_adapter = LLMToolAdapter(config)
        budget = getattr(config, "agent_deep_research_budget", 30000)

        agent = ResearchAgent(
            tool_registry=registry,

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Inspect get_config().is_agent_available() (or the /agent capability endpoint if exposed) before offering the research feature in the client
  2. Set AGENT_MODE=true and configure at least one agent-safe LLM route in .env (check .env.example for the exact variable names), then restart the service
  3. Verify the running process actually loaded the intended .env (container rebuild, working directory, dotenv path) since stale config is a frequent cause
  4. If agent mode is intentionally disabled, catch HTTP 400 in the caller and hide/disable the research UI instead of retrying

Example fix

// before
const res = await fetch('/api/v1/agent/research', {method:'POST', body: JSON.stringify({question})});
if (!res.ok) throw new Error(await res.text());

// after
const res = await fetch('/api/v1/agent/research', {method:'POST', body: JSON.stringify({question})});
if (res.status === 400) {
  const body = await res.json();
  if (body.detail === 'Agent mode is not enabled') {
    // surface capability info instead of an error
    return { unavailable: true, reason: 'agent_mode_disabled' };
  }
}
if (!res.ok) throw new Error(await res.text());
Defensive patterns

Strategy: validation

Validate before calling

# Python client: check capability before calling
import requests

caps = requests.get(f"{BASE}/api/v1/agent/capabilities").json()  # or read config
if not caps.get("agent_available", False):
    disable_research_ui()
else:
    requests.post(f"{BASE}/api/v1/agent/research", json={"question": q})

Try / catch

try:
    resp = post_research(question)
except HTTPError as e:
    if e.response.status_code == 400 and 'Agent mode is not enabled' in e.response.text:
        mark_agent_unavailable()  # permanent config state, do not retry
    else:
        raise

Prevention

When it happens

Trigger: Calling POST /agent/research with AGENT_MODE explicitly set to false; or AGENT_MODE unset/true but no LLM provider configured that the config classifies as agent-safe (e.g. only non-agent routes in .env). The check fires before any question processing, so every request with that config fails identically.

Common situations: Fresh deployments where .env only contains basic analysis keys but no agent-capable model route; CI or Docker containers built without the agent env vars; a user disabling AGENT_MODE to save tokens and later forgetting it when calling the research endpoint; model/route renames that make the previously agent-safe route no longer match.

Related errors


AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15). Data as JSON: /api/errors/a33a5aa738f2ed01. Report an issue: GitHub.