Class OpenRouterClient

All Implemented Interfaces:
com.tnsai.llm.LLMClient

public class OpenRouterClient extends AbstractOpenAICompatibleClient
OpenRouter LLM client implementation. Provides unified access to 300+ models from 60+ providers via OpenRouter's API.

OpenRouter speaks the OpenAI /chat/completions wire format, so this client is a thin subclass of AbstractOpenAICompatibleClient; its only divergence is a set of optional ranking / routing headers attached via addProviderHeaders(Request.Builder).

Model naming convention: "provider/model-name"

  • openai/gpt-4o - OpenAI GPT-4o
  • anthropic/claude-3.5-sonnet - Anthropic Claude 3.5 Sonnet
  • google/gemini-pro-1.5 - Google Gemini Pro 1.5

Configuration via environment variables:

  • OPENROUTER_API_KEY - Required API key for authentication
  • OPENROUTER_SITE_URL - Optional, your site URL for ranking
  • OPENROUTER_SITE_NAME - Optional, your site name for ranking
See Also:
  • Field Details

    • BETA_FINE_GRAINED_TOOL_STREAMING

      public static final String BETA_FINE_GRAINED_TOOL_STREAMING
      Beta header that opts into Anthropic's fine-grained tool streaming. Only meaningful when the underlying model is Claude — OpenRouter passes it through to Anthropic when routing to a Claude model.
      Since:
      0.5.0 (issue #26)
      See Also:
  • Constructor Details

    • OpenRouterClient

      public OpenRouterClient(String model)
    • OpenRouterClient

      public OpenRouterClient(String model, float temperature)
    • OpenRouterClient

      public OpenRouterClient(String model, float temperature, Integer maxTokens)
    • OpenRouterClient

      public OpenRouterClient(String model, float temperature, Float topP, Integer maxTokens)
    • OpenRouterClient

      public OpenRouterClient(String model, float temperature, Integer maxTokens, String apiKey, String siteUrl, String siteName)
    • OpenRouterClient

      public OpenRouterClient(String model, float temperature, Float topP, Integer maxTokens, String apiKey, String siteUrl, String siteName)
  • Method Details

    • setFineGrainedToolStreaming

      public OpenRouterClient setFineGrainedToolStreaming(boolean enabled)
      Explicit override for the auto-detected fine-grained tool streaming flag (#26). Returns this for fluent configuration.

      Use to opt OUT on a Claude model (you've measured it makes things worse for your workload) or to opt IN on a non-Claude model id that still routes through Anthropic via OpenRouter's routing tags.

      Since:
      0.5.0 (issue #26)
    • isFineGrainedToolStreamingEnabled

      public boolean isFineGrainedToolStreamingEnabled()
      Whether the fine-grained tool streaming beta header will be sent (#26).