Skip to content

[BUG] API credentials not refreshed for advisor_agent #109

Open
@Mgomez-01

Description

@Mgomez-01

Description

LLM API credentials in environment variables are not properly refreshed for advisor_agent after being updated through the UI. The agent continues to use stale credentials from when the module was first imported, resulting in authentication failures with the LLM provider.

Steps to Reproduce

  1. Start the Docker container with the Archon application
  2. Navigate to the Environment tab
  3. Set OpenAI API key and other necessary environment variables
  4. Switch to the Chat tab
  5. Send a test message
  6. Observe authentication error in the chat window and in docker logs

Expected Behavior

After setting the API key in the Environment tab, the application should use the updated credentials for all agent interactions, including the advisor_agent.

Actual Behavior

added debug prints in graph and advisor py files.
The main archon_graph module correctly picks up the new API key:

DEBUG GRAPH - Using API key (first few chars): sk-proj-...
DEBUG GRAPH - Provider: OpenAI  
DEBUG GRAPH - Base URL: https://api.openai.com/v1

However, the advisor_agent continues to use stale credentials from when it was first loaded:

DEBUG ADVISOR - Using API key (first few chars): no-llm-a...
DEBUG ADVISOR - Provider: OpenAI
DEBUG ADVISOR - Base URL: https://api.openai.com/v1

This results in authentication errors when trying to chat with the agent:

Traceback (most recent call last):
...
openai.AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: no-llm-a***********ided. You can find your API key at https://platform.openai.com/account/api-keys.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_api_key'}}
During task with name 'advisor_with_examples' and id '55a3a099-421c-adbe-3052-a2fe736bf0a5'

Screenshots

Image

Environment

  • OS: Running in Docker container
  • Python Version: Python 3.12
  • Using Streamlit for UI

Additional Context

  • This happens consistently every time an API key is updated through the Environment tab
  • The issue is specific to modules that create agents at import time, such as advisor_agent.py
  • The main archon_graph.py module correctly uses updated credentials, but imported modules don't refresh their state
  • The environment variables are correctly saved to workbench/env_vars.json and can be verified:
cat workbench/env_vars.json
{
  "profiles": {
    "default": {
      "LLM_PROVIDER": "OpenAI",
      "EMBEDDING_PROVIDER": "OpenAI",
      "BASE_URL": "https://api.openai.com/v1",
      "LLM_API_KEY": "sk-proj-......EA",
      "EMBEDDING_BASE_URL": "https://api.openai.com/v1",
      "EMBEDDING_API_KEY": "sk-proj-......EA"
    }
  },
  "current_profile": "default"
}

Possible Solution

The issue was fixed by implementing a factory pattern for the advisor_agent:

  1. In advisor_agent.py:

    • Remove global agent initialization
    • Create a factory function that builds a fresh agent with current environment variables
  2. In archon_graph.py:

    • Import only the necessary types from advisor_agent.py
    • Create a fresh advisor agent instance each time it's needed using the factory function

This pattern ensures that every time an agent is used, it's created with the most current environment variables rather than using stale ones from module import time.

Currently running this fix on my fork. shown below is one failed message before fix and another after update

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions