Skip to main content

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​

VariableDefaultDescription
CORTEXHUB_ROUTER_URLhttps://api.cortexhub.ai/v1AI gateway base URL
CORTEXHUB_API_KEYnoneAgent API key with ai scope

The MCP OAuth credential path does not provide access to the AI gateway.

Errors​

StatusMeaningFix
400End-user subject missingSupply end_user_subject when creating the API-key session
401Missing or invalid bearer credentialCheck CORTEXHUB_API_KEY
403Key lacks ai scope or is not bound to an agentGrant the agent the router capability and mint the matching scope