LLM router
The Python SDK provides an OpenAI-compatible client for the CortexHub AI gateway.
The API key must have the AI gateway scope and be bound to a registered external agent. Keyed inference also requires an end-user subject.
Install
pip install "cortexhub[ai]"
Configure and call
export CORTEXHUB_API_KEY="cxh_key_..."
from cortexhub import Cortexhub
cx = Cortexhub()
session = cx.session(
agent="your-registered-agent-reference",
mcp_session_id="conversation-8472",
end_user_subject="user-193",
)
result = session.llm.responses.create(
model="cortexhub/auto",
input="Summarize our refund policy in two lines.",
)
print(result.output_text)
cortexhub/auto lets CortexHub route each turn. Chat Completions, streaming, and model listing are also available on session.llm.
Frameworks
For a framework that accepts an OpenAI-compatible client, use the session-scoped client or configure the gateway with the session attribution headers. See Framework examples.
Configuration
| Variable | Default | Description |
|---|---|---|
| CORTEXHUB_ROUTER_URL | https://api.cortexhub.ai/v1 | AI gateway base URL |
| CORTEXHUB_API_KEY | none | Agent API key with ai scope |
The MCP OAuth credential path does not provide access to the AI gateway.
Errors
| Status | Meaning | Fix |
|---|---|---|
| 400 | End-user subject missing | Supply end_user_subject when creating the API-key session |
| 401 | Missing or invalid bearer credential | Check CORTEXHUB_API_KEY |
| 403 | Key lacks ai scope or is not bound to an agent | Grant the agent the router capability and mint the matching scope |