LLM Gateways

Route agent LLM calls through unified gateways like OpenRouter and LiteLLM Proxy for access to 100+ providers

Route agent LLM calls through OpenRouter, LiteLLM Proxy, or a custom OpenAI-compatible gateway — no manual registration required.

python
import os
from praisonaiagents import Agent

if not os.getenv("OPENROUTER_API_KEY"):
    raise EnvironmentError("Set OPENROUTER_API_KEY in your environment")

agent = Agent(
    name="assistant",
    llm="openrouter/anthropic/claude-3.5-sonnet",
)
agent.start("Hello!")

The user picks a gateway-backed model string; the agent routes the request through the gateway to the underlying provider.

Diagram

Agent

Gateway Provider

OpenRouter

LiteLLM Proxy

Custom Endpoint

Quick Start

  1. OpenRouter — one line
    python
    import os
    from praisonaiagents import Agent
    
    if not os.getenv("OPENROUTER_API_KEY"):
        raise EnvironmentError("Set OPENROUTER_API_KEY in your environment")
    
    agent = Agent(
        name="assistant",
        llm="openrouter/anthropic/claude-3.5-sonnet",
    )
    agent.start("Hello!")

    Gateway providers auto-register on import — no register_gateway_providers() call needed.

  2. LiteLLM Proxy
    python
    import os
    from praisonaiagents import Agent
    
    os.environ["LITELLM_PROXY_BASE_URL"] = "http://localhost:4000"
    if not os.getenv("LITELLM_PROXY_API_KEY"):
        raise EnvironmentError("Set LITELLM_PROXY_API_KEY in your environment")
    
    agent = Agent(
        name="assistant",
        llm="litellm-proxy/gpt-4",
    )
    agent.start("Hello!")
  3. Custom gateway (advanced)
    python
    import os
    from praisonai.llm import create_llm_provider
    from praisonaiagents import Agent
    
    provider = create_llm_provider({
        "name": "custom-gateway",
        "model_id": "my-model",
        "config": {
            "base_url": "https://api.example.com/v1",
            "api_key": os.getenv("CUSTOM_GATEWAY_API_KEY", ""),
        },
    })
    agent = Agent(name="assistant", llm=provider)
    agent.start("Hello!")

Choose Your Gateway

Diagram

100+ hosted models

Self-hosted caching & fallbacks

Internal OpenAI-compatible API

What do you need?

OpenRouter

LiteLLM Proxy

Custom Gateway

NeedGatewayExample llm
Hosted access to 100+ modelsOpenRouteropenrouter/anthropic/claude-3.5-sonnet or or/gpt-4
Self-hosted caching, fallback, load balancingLiteLLM Proxylitellm-proxy/gpt-4
Internal OpenAI-compatible endpointCustom Gatewaycustom-gateway/my-model with base_url in config

Route Gateway Models by Cost

Use custom ModelProfile entries when one OpenAI-compatible gateway exposes multiple models. The router selects a model name from your metadata; LLM then sends the request to the shared endpoint.

Diagram

Task

Model Router

Selected Model

OpenAI-compatible Gateway

  1. Set gateway credentials

    Keep the endpoint and key outside your source code:

    bash
    export OPENAI_BASE_URL="${OPENAI_BASE_URL:?Set OPENAI_BASE_URL in your shell}"
    export OPENAI_API_KEY="${OPENAI_API_KEY:?Set OPENAI_API_KEY in your shell}"
    export GATEWAY_FAST_MODEL="${GATEWAY_FAST_MODEL:?Set GATEWAY_FAST_MODEL in your shell}"
    export GATEWAY_REASONING_MODEL="${GATEWAY_REASONING_MODEL:?Set GATEWAY_REASONING_MODEL in your shell}"
  2. Describe the available models

    Replace the placeholder capabilities and costs with values published by your gateway. PraisonAI does not maintain gateway-specific pricing.

    python
    import os
    
    from praisonaiagents import Agent
    from praisonaiagents.llm import (
        LLM,
        ModelProfile,
        ModelRouter,
        TaskComplexity,
    )
    
    gateway_url = os.environ["OPENAI_BASE_URL"]
    gateway_key = os.environ["OPENAI_API_KEY"]
    fast_model = os.environ["GATEWAY_FAST_MODEL"]
    reasoning_model = os.environ["GATEWAY_REASONING_MODEL"]
    
    models = [
        ModelProfile(
            name=f"openai/{fast_model}",
            provider="gateway",
            complexity_range=(TaskComplexity.SIMPLE, TaskComplexity.MODERATE),
            cost_per_1k_tokens=0.001,
            strengths=["speed", "summarization"],
            capabilities=["text"],
            context_window=32_000,
        ),
        ModelProfile(
            name=f"openai/{reasoning_model}",
            provider="gateway",
            complexity_range=(TaskComplexity.MODERATE, TaskComplexity.VERY_COMPLEX),
            cost_per_1k_tokens=0.01,
            strengths=["reasoning", "analysis"],
            capabilities=["text", "function-calling"],
            context_window=128_000,
        ),
    ]
    
    task = "Summarize this support conversation"
    router = ModelRouter(models=models)
    selected_model = router.select_model(task, budget_conscious=True)
    
    llm = LLM(
        model=selected_model,
        base_url=gateway_url,
        api_key=gateway_key,
    )
    agent = Agent(name="assistant", llm=llm)
    agent.start(task)

How It Works

Diagram
Underlying LLMGateway ServiceLiteLLMGatewayProviderAgentUserUnderlying LLMGateway ServiceLiteLLMGatewayProviderAgentUserpromptgenerate()completion()HTTP requestroute to providerresponse

Provider Reference

OpenRouter

OptionTypeDefaultDescription
api_keystr$OPENROUTER_API_KEYOpenRouter API key
base_urlstrhttps://openrouter.ai/api/v1API endpoint
extra_headersdict—e.g. X-Title, HTTP-Referer for app ranking

Aliases: openrouter, or. Model IDs are prefixed with openrouter/ automatically unless already prefixed.

LiteLLM Proxy

OptionTypeDefaultDescription
api_keystr$LITELLM_PROXY_API_KEYProxy API key
base_urlstr$LITELLM_PROXY_BASE_URL or http://localhost:4000Proxy URL
extra_headersdict—Custom auth or routing headers

Aliases: litellm-proxy, llm-proxy, litellm-gateway. Model IDs are passed through unchanged.

Custom Gateway

OptionTypeDefaultDescription
base_urlstrrequiredOpenAI-compatible endpoint — raises ValueError if missing
api_keystr—Optional API key
extra_headersdict—Custom headers

Aliases: custom-gateway, gateway, custom.

Explicit config["api_key"] overrides environment variables at init time.


Common Patterns

Multi-agent, different gateways:

python
from praisonaiagents import Agent

researcher = Agent(name="researcher", llm="openrouter/anthropic/claude-3.5-sonnet")
writer = Agent(name="writer", llm="litellm-proxy/gpt-4o-mini")

OpenRouter attribution headers:

python
from praisonai.llm import OpenRouterProvider
from praisonaiagents import Agent

provider = OpenRouterProvider("gpt-4", config={
    "extra_headers": {"X-Title": "My App", "HTTP-Referer": "https://myapp.com"},
})
agent = Agent(name="assistant", llm=provider)

Shorthand alias:

python
agent = Agent(name="assistant", llm="or/gpt-4")  # same as openrouter/gpt-4

Developer Flow

Diagram

Install praisonai

Set env var

Agent with llm= gateway/model

agent.start


Best Practices

Prefer environment variables

Set OPENROUTER_API_KEY or LITELLM_PROXY_API_KEY instead of hardcoding keys in config.

OpenRouter for experimentation

Quick access to 100+ models with one API key — ideal for prototyping.

LiteLLM Proxy for production

Self-hosted caching, observability, and fallback routing suit production workloads.

Custom Gateway for compliance

Use CustomGatewayProvider when traffic must stay on an internal OpenAI-compatible proxy.

extra_headers for attribution

OpenRouter uses X-Title and HTTP-Referer for app ranking and routing hints.


Related