Skip to content

OpenRouter Conversation Example

This page embeds the full source for examples/openrouter_conversation.py.

It includes a short fallback configuration example:

  • Primary model comes from LLM_MODEL
  • If the primary fails, it falls back to openai/gpt-4o-mini
python
"""Example: call a hosted model through the OpenRouter-backed OpenAI-compatible client and ConversationRunner."""

from swarmforge.env import require_env_vars
from swarmforge.evaluation.provider import ModelConfig, OpenAIClientWrapper
from swarmforge.evaluation.runner.conversation import ConversationRunner


def main():
    env = require_env_vars(
        "MODEL_PROVIDER",
        "LLM_MODEL",
        "OPENROUTER_API_KEY",
        "OPENROUTER_SITE_URL",
        "OPENROUTER_APP_NAME",
    )
    if env["MODEL_PROVIDER"].lower() != "openrouter":
        raise ValueError("openrouter_conversation.py requires MODEL_PROVIDER=openrouter in .env")

    # Short fallback example: if the primary model fails, try the listed models in order.
    model_config = ModelConfig(fallback_models=["openai/gpt-4o-mini"])
    client = OpenAIClientWrapper(model_config)
    runner = ConversationRunner(
        client=client,
        system_prompt="You are a concise assistant.",
    )
    trace = runner.run_conversation(
        suite_id="openrouter-example",
        case_id="single-turn",
        conversation=[
            {
                "user_input": "Summarize why OpenRouter is useful for a multi-agent SDK in one paragraph.",
            }
        ],
    )
    print(trace.turns[-1].assistant_response)


if __name__ == "__main__":
    main()

Run:

bash
python examples/openrouter_conversation.py

Released as open source.