Nexus Basic
Run Claude Sonnet 4.6 through a local Nexus gateway in sync, async, and streaming modes.
The source routes requests to Anthropic's retired claude-sonnet-4-20250514 model. Replace it with anthropic/claude-sonnet-4-6 before running. See Anthropic model deprecations.
"""
Nexus Basic
===========
Cookbook example for `nexus/basic.py`.
"""
from agno.agent import Agent, RunOutput # noqa
from agno.models.nexus import Nexus
import asyncio
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(model=Nexus(id="anthropic/claude-sonnet-4-20250514"), markdown=True)
# Get the response in a variable
# run: RunOutput = agent.run("Share a 2 sentence horror story")
# print(run.content)
# Print the response in the terminal
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
# --- Sync ---
agent.print_response("Share a 2 sentence horror story")
# --- Sync + Streaming ---
agent.print_response("Share a 2 sentence horror story", stream=True)
# --- Async ---
asyncio.run(agent.aprint_response("Share a 2 sentence horror story"))
# --- Async + Streaming ---
asyncio.run(agent.aprint_response("Share a 2 sentence horror story", stream=True))Run the Example
Configure the gateway in the first terminal
Install Nexus. Create nexus.toml with this loopback-only configuration:
[server]
listen_address = "127.0.0.1:8000"
[llm]
enabled = true
[llm.protocols.openai]
enabled = true
path = "/llm"
[llm.providers.anthropic]
type = "anthropic"
api_key = "{{ env.ANTHROPIC_API_KEY }}"
[llm.providers.anthropic.models."claude-sonnet-4-6"]Start the gateway in the first terminal
Set an Anthropic API key with access to Sonnet 4.6. The gateway process uses this key for upstream model requests.
export ANTHROPIC_API_KEY="your_anthropic_api_key_here"From the directory containing nexus.toml, run nexus. Leave this foreground process running and open a second terminal in your example directory for the remaining steps.
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall the client dependencies in the second terminal
uv pip install -U agno openaiConfigure the current client model
Replace anthropic/claude-sonnet-4-20250514 with anthropic/claude-sonnet-4-6 and add api_key="nexus-local" to Nexus(...) in the saved file. The default URL matches this gateway's http://localhost:8000/llm/v1/ route.
This TOML does not enable client OAuth. The nonempty dummy bearer satisfies the OpenAI SDK; it is not an OpenAI provider key and does not secure the gateway. An OAuth-protected deployment instead needs its required client access token. The Anthropic provider key remains in the gateway terminal.
Run the example in the second terminal
Save the code above as basic.py, then run:
python basic.pyFull source: cookbook/90_models/nexus/basic.py