Openrouter import gives openai api error

from agno.agent import Agent, RunResponse
from agno.models.openrouter import OpenRouter

agent = Agent(
    model=OpenRouter(id="qwen/qwen3-coder:free"),
    markdown=True
)

# Print the response in the terminal
agent.print_response("Share a 2 sentence horror story.")

ERROR    OPENAI_API_KEY not set. Please set the OPENAI_API_KEY environment variable.
ERROR    Error from OpenAI API: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable     
WARNING  Attempt 1/1 failed: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable        
ERROR    Failed after 1 attempts. Last error using OpenRouter(qwen/qwen3-coder:free)
β–°β–±β–±β–±β–±β–±β–± Thinking...
┏━ Message ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃                                                                                                                                                                      ┃
┃ Share a 2 sentence horror story.                                                                                                                                     ┃
┃                                                                                                                                                                      ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
Traceback (most recent call last):
  File "C:\Users\prajw\Downloads\AI-OS\aios\Lib\site-packages\agno\models\openai\chat.py", line 328, in invoke
    return self.get_client().chat.completions.create(
           ~~~~~~~~~~~~~~~^^
  File "C:\Users\prajw\Downloads\AI-OS\aios\Lib\site-packages\agno\models\openai\chat.py", line 126, in get_client
    return OpenAIClient(**client_params)
  File "C:\Users\prajw\Downloads\AI-OS\aios\Lib\site-packages\openai\_client.py", line 126, in __init__
    raise OpenAIError(
        "The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable"
    )
openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\Users\prajw\Downloads\AI-OS\python-backend\test.py", line 10, in <module>
    agent.print_response("Share a 2 sentence horror story.")
    ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\prajw\Downloads\AI-OS\aios\Lib\site-packages\agno\agent\agent.py", line 6983, in print_response
    run_response = self.run(
        message=message,
    ...<10 lines>...
        **kwargs,
    )
  File "C:\Users\prajw\Downloads\AI-OS\aios\Lib\site-packages\agno\agent\agent.py", line 1117, in run
    raise last_exception
  File "C:\Users\prajw\Downloads\AI-OS\aios\Lib\site-packages\agno\agent\agent.py", line 1075, in run
    response = self._run(
        run_response=run_response,
    ...<5 lines>...
        messages=messages,
    )
  File "C:\Users\prajw\Downloads\AI-OS\aios\Lib\site-packages\agno\agent\agent.py", line 696, in _run
    model_response: ModelResponse = self.model.response(
                                    ~~~~~~~~~~~~~~~~~~~^
        messages=run_messages.messages,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<4 lines>...
        response_format=response_format,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\prajw\Downloads\AI-OS\aios\Lib\site-packages\agno\models\base.py", line 337, in response
    assistant_message, has_tool_calls = self._process_model_response(
                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        messages=messages,
        ^^^^^^^^^^^^^^^^^^
    ...<3 lines>...
        tool_choice=tool_choice or self._tool_choice,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\prajw\Downloads\AI-OS\aios\Lib\site-packages\agno\models\base.py", line 539, in _process_model_response
    response = self.invoke(
        messages=messages,
    ...<2 lines>...
        tool_choice=tool_choice or self._tool_choice,
    )
  File "C:\Users\prajw\Downloads\AI-OS\aios\Lib\site-packages\agno\models\openai\chat.py", line 369, in invoke
    raise ModelProviderError(message=str(e), model_name=self.name, model_id=self.id) from e
agno.exceptions.ModelProviderError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable 

Hi @Prajwal, thanks for reaching out and supporting Agno. I’ve shared this with the team, we’re working through all requests one by one and will get back to you soon.If it’s urgent, please let us know. We appreciate your patience!

1 Like

Hi @Prajwal

The Agent is falling back to default OpenAI models when your selected model API key is not exported or available in your env.

I was able to recreate and fix your error by unsetting all of my API keys. Running the code I get the same error. Then, doing export OPENROUTER_API_KEY=”***”it works correctly. Please make sure that you have exported it.

oh when i save the api in windows env variable it works but when i save in .env and import dotenv its not working.

Hi @Prajwal
Happy that it got resolved. Probably not working in windows due to how env vars are handled.
How are you importing your .env file?

You can try this at the start of your script

```
from dotenv import load_dotenv

load_dotenv()
```

yup i tried this

```
from dotenv import load_dotenv

load_dotenv()
```

but still doesnt work.

does restarting the pc would solve this ?