Skip to content

spawn 子 agent 报 401: OpenRouter API key 未传递给 litellm #281

Closed

Description

@arelchan

Bug 描述

使用 spawn 创建子 agent 时,子 agent 调用 litellm → OpenRouter 时报 401 User not found,无法正常执行任务。

根因分析

  1. Raven 主进程从 config.json 的 providers.openrouter.apiKey 读取 API key,主会话正常
  2. spawn 出的子 agent 通过 litellm 调用 LLM
  3. litellm 从环境变量 OPENROUTER_API_KEY 读取 API key
  4. 当前环境没有设置 OPENROUTER_API_KEY 环境变量
  5. litellm 带着空 key 发请求 → OpenRouter 返回 401 User not found

复现步骤

raven config 中 provider 设置为 openrouter
执行任意 spawn 操作
→ 子 agent 立即报错: 401 User not found

日志

~/.raven/workspace/sessions/sub/2064bdc3.jsonl:

{"role": "assistant", "content": "Error calling LLM: litellm.AuthenticationError: AuthenticationError: OpenrouterException - {\"error\":{\"message\":\"User not found.\",\"code\":401}}"}

建议修复

spawn 子进程时,应该将 providers.<provider>.apiKey 注入为对应 provider 的环境变量:

  • OpenRouter → OPENROUTER_API_KEY
  • Anthropic → ANTHROPIC_API_KEY
  • OpenAI → OPENAI_API_KEY

临时 workaround

export OPENROUTER_API_KEY=$(raven config show | python3 -c "import json,sys; print(json.load(sys.stdin)['providers']['openrouter']['apiKey'])")

环境

  • Raven v0.1.10 / macOS arm64 / Python 3.12.13
  • Provider: OpenRouter (deepseek-v4-pro)

Activity

  1. 0xKT commented on Oct 9, 2026

    @0xKT
    Member

    Closing as fixed on main. The env-var theory does not hold: the configured apiKey is passed to LiteLLM explicitly on every call (pinned by regression tests in #287, 5719cbe), and on current main a spawned sub-agent sends the key from config.json with OPENROUTER_API_KEY unset. The reported pattern, a sub-agent getting 401 while the main session works, most likely came from the subagent manager keeping its startup provider across a live model switch; #282 (a756a72) fixed that, and #284 (e26e0e1) now runs every spawn made during a turn on that turn's model binding. A direct chat with a built-in sub-agent instance (a later feature) still resolves the default model rather than the session's switch; that separate defect is tracked in #888. If the spawn 401 itself still reproduces on the latest release, please reopen with the model and provider config (key redacted).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions