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!
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.