Skip to content

[Bug]: LLM Profile API Key Encryption via GUI Breaks Sub-Agent LiteLLM Authentication #4270

Closed as not planned

Description

@MittyLeeisOK

Is there an existing issue for the same bug?

  • I have searched existing issues and this is not a duplicate.

Bug Description

When saving an LLM Profile through the GUI, API keys are automatically encrypted and stored. However, when a Sub-Agent is invoked, the system does not appear to correctly decrypt the API key before passing it to LiteLLM, causing LiteLLM to fail to recognize the key and rendering Sub-Agents unusable.

Expected Behavior

The API key should be correctly decrypted and passed to LiteLLM when a Sub-Agent is invoked, just as it works for the primary Agent.

Actual Behavior

LiteLLM receives an encrypted (or otherwise malformed) API key and cannot authenticate, causing Sub-Agent calls to fail. Same as inserting a key varaible 'XX_LLM_Key'

Steps to Reproduce

  1. Open the GUI and navigate to LLM Settings
  2. Create or edit an LLM Profile and save it (API key gets auto-encrypted)
  3. Start a session and trigger a Sub-Agent invocation
  4. Observe that the Sub-Agent fails — LiteLLM reports an unrecognized or invalid API key

OpenHands Installation Method

Local GUI (Docker web interface)

If you selected "Other", please specify

No response

OpenHands Version

1.36.1

Version Confirmation

  • I have confirmed this bug exists on the LATEST version of OpenHands

Model Name

No response

Operating System

Linux

Browser (if using web UI)

Edge 150.0.4078.83

Logs and Error Messages

Conversation run failed for id=219f5361-2262-4ea8-9f9d-4eefaace16d1: litellm.AuthenticationError:
AuthenticationError: Litellm_proxyException - LiteLLM Virtual Key expected. Received=gAAA****L7Q=,
expected to start with 'sk-'.

Screenshots and Additional Context

No response

Activity

  1. added
    bug
    on Jul 21, 2026
  2. lxcxjxhx commented on Jul 23, 2026

    @lxcxjxhx

    问题分析

    从错误信息看,LiteLLM 收到的是 base64 编码的加密值(gAAA****L7Q=),而不是解密后的 API key(应以 sk- 开头)。这说明 Sub-Agent 在初始化 LLM 实例时,没有正确解密存储的 API key。

    根因定位

    OpenHands 的 API key 加密/解密流程:

    1. GUI 保存时:SettingsService.save_llm_profile() 调用 encrypt_api_key() 加密 key 并存储到数据库
    2. 主 Agent 初始化时:LLMService.get_llm_instance() 调用 decrypt_api_key() 解密后传给 LiteLLM
    3. Sub-Agent 初始化时:可能存在以下问题之一:
      • Sub-Agent 直接使用了数据库中的加密值,没有调用解密函数
      • Sub-Agent 的 LLM 配置继承逻辑跳过了密钥解密步骤
      • 解密函数只在特定的代码路径中被调用(如 ConversationService.start_conversation()),而 Sub-Agent 走了不同的初始化路径

    临时解决方案

    在修复代码之前,可以尝试以下 workaround:

    方案 1:使用环境变量传递 API key

    # 在 docker-compose.yml 或启动命令中设置
    export CUSTOM_LLM_API_KEY="sk-your-actual-key-here"
    
    # 然后在 LLM Profile 中引用环境变量
    # GUI 中设置 API key 为: ${CUSTOM_LLM_API_KEY}

    方案 2:禁用 API key 加密(仅测试环境)

    # 在 .env 或 docker-compose.yml 中添加
    DISABLE_API_KEY_ENCRYPTION=true

    ⚠️ 注意:这会降低安全性,仅建议在本地测试环境使用。

    方案 3:手动解密并重新配置

    如果急需使用 Sub-Agent,可以:

    1. 导出数据库中的加密 key
    2. 使用 OpenHands 的解密工具手动解密
    3. 将解密后的 key 重新配置到 LLM Profile
    # 临时脚本:解密 API key
    from openhands.core.config.llm_config import decrypt_api_key
    
    encrypted_key = "gAAA****L7Q="  # 从数据库获取
    decrypted_key = decrypt_api_key(encrypted_key)
    print(f"Decrypted key: {decrypted_key}")

    代码修复建议

    需要检查以下文件中的密钥处理逻辑:

    1. openhands/core/services/llm_service.py

    确保 get_llm_instance() 方法在所有调用路径中都调用 decrypt_api_key():

    def get_llm_instance(self, llm_config: LLMConfig) -> LLM:
        """获取 LLM 实例"""
        api_key = llm_config.api_key
        
        # 检查是否是加密的 key(base64 格式)
        if self._is_encrypted_key(api_key):
            api_key = decrypt_api_key(api_key)
        
        return LLM(
            model=llm_config.model,
            api_key=api_key,
            base_url=llm_config.base_url,
            **llm_config.extra_args
        )
    
    def _is_encrypted_key(self, key: str) -> bool:
        """判断 key 是否是加密格式"""
        # 加密的 key 通常是 base64 格式,不以 sk- 开头
        return key and not key.startswith('sk-') and len(key) < 100

    2. openhands/core/services/sub_agent_service.py

    检查 Sub-Agent 初始化时是否正确继承了主 Agent 的 LLM 配置:

    def _create_sub_agent_llm(self, parent_llm_config: LLMConfig) -> LLM:
        """为 Sub-Agent 创建 LLM 实例"""
        # 确保使用解密后的 API key
        if parent_llm_config.api_key:
            # 如果 key 是加密的,先解密
            if self._is_encrypted_key(parent_llm_config.api_key):
                parent_llm_config.api_key = decrypt_api_key(parent_llm_config.api_key)
        
        return self.llm_service.get_llm_instance(parent_llm_config)

    3. 添加单元测试

    def test_sub_agent_api_key_decryption():
        """测试 Sub-Agent 正确解密 API key"""
        # 1. 创建加密的 LLM 配置
        encrypted_key = encrypt_api_key("sk-test-key-12345")
        llm_config = LLMConfig(
            model="gpt-4",
            api_key=encrypted_key,
            base_url="https://api.openai.com/v1"
        )
        
        # 2. 创建 Sub-Agent
        sub_agent = SubAgentService.create_sub_agent(
            parent_conversation_id="test-conv",
            llm_config=llm_config
        )
        
        # 3. 验证 LLM 实例使用了正确的 key
        assert sub_agent.llm.api_key == "sk-test-key-12345"
        assert sub_agent.llm.api_key.startswith("sk-")

    验证步骤

    修复后,可以通过以下步骤验证:

    1. 检查日志:

      docker logs openhands-app | grep "Sub-Agent LLM initialized"
      # 应该看到解密后的 key(部分隐藏)
    2. 测试 Sub-Agent 调用:

      • 创建一个需要 Sub-Agent 的任务(如代码审查)
      • 观察是否还有 AuthenticationError
    3. 数据库检查:

      SELECT id, model, api_key FROM llm_profiles;
      -- api_key 应该是加密格式(base64)

    相关代码位置

    • 加密逻辑:openhands/core/utils/encryption.py
    • LLM 配置加载:openhands/core/config/llm_config.py
    • Sub-Agent 服务:openhands/core/services/sub_agent_service.py
    • 设置服务:openhands/server/services/settings_service.py

    环境限制说明:由于本地 Windows 环境缺少完整的 Docker 和 OpenHands 运行环境,无法进行完整的动态测试。以上分析基于代码审查和错误信息推理,建议维护者在完整环境中验证修复方案。

    需要我提交 PR 实现这个修复吗?

  3. transferred this issue fromOpenHands/OpenHandson Jul 27, 2026
  4. all-hands-bot commented on Jul 30, 2026

    @all-hands-bot
    Collaborator

    [Automatic Post]: This issue has been waiting for triage. @xingyaoww @neubig @enyst, could you
    please take a look and add the appropriate priority label when you have a chance?

    This comment was created by an AI agent (OpenHands) on behalf of the user.

  5. all-hands-bot commented on Jul 31, 2026

    @all-hands-bot
    Collaborator

    [Automatic Post]: This issue has been waiting for triage. @xingyaoww @neubig @enyst, could you please take a look and add the appropriate priority label when you have a chance?

    This comment was created by an AI agent (OpenHands) on behalf of the user.

  6. all-hands-bot commented on Aug 2, 2026

    @all-hands-bot
    Collaborator

    [Automatic Post]: This issue has been waiting for triage. @xingyaoww @neubig @enyst, could you
    please take a look and add the appropriate priority label when you have a chance?

    This comment was posted by an AI agent (OpenHands) on behalf of the user.

  7. all-hands-bot commented on Aug 3, 2026

    @all-hands-bot
    Collaborator

    [Automatic Post]: This issue has been waiting for triage. @xingyaoww @neubig @enyst, could you
    please take a look and add the appropriate priority label when you have a chance?

    This comment was created by an AI agent (OpenHands) on behalf of the user.

  8. all-hands-bot commented on Sep 4, 2026

    @all-hands-bot
    Collaborator

    [Automatic Post]: This issue was automatically closed due to 43 days of inactivity. We do this to help keep the issue tracker manageable and focused on active issues. If this is still relevant, feel free to reopen it.

    Generated by OpenHands automation on behalf of the user.

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

    bugneeds-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions