OpenRouter TypeScript SDK - v1.0.6
    Preparing search index...

    Class OpenRouterClient

    Index

    Chat Completions

    • Executes multiple chat completion requests in parallel (batch) Automatically handles rate limiting and individual errors

      Parameters

      Returns Promise<BatchResult[]>

      Results of each request (success or error)

      const results = await client.batchChatCompletion([
      { model: 'openai/gpt-3.5-turbo', messages: [{ role: 'user', content: 'Q1' }] },
      { model: 'openai/gpt-3.5-turbo', messages: [{ role: 'user', content: 'Q2' }] }
      ], { maxConcurrent: 5, stopOnError: false });

      results.forEach((result, idx) => {
      if (result.success && result.response) {
      console.log(`Request ${idx}: ${result.response.choices[0].message.content}`);
      } else {
      console.error(`Request ${idx} failed: ${result.error?.message}`);
      }
      });

    Validation & Helpers

    • Quickly checks if a model supports a specific feature

      Parameters

      • modelId: string

        The model ID to check

      • feature: ModelFeature

        The feature to check ('streaming', 'tools', 'vision', 'json')

      Returns Promise<boolean>

      True if the model supports the feature

      const supportsVision = await client.supportsFeature('anthropic/claude-3.5-sonnet', 'vision');
      if (supportsVision) {
      // Send an image
      }
    • Validates request parameters against model capabilities

      Parameters

      • modelId: string

        The model ID

      • params: {
            messages?: ChatMessage[];
            stream?: boolean;
            tools?: any[];
            response_format?: any;
            max_tokens?: number;
            [key: string]: any;
        }

        The parameters to validate

      Returns Promise<ValidationResult>

      Validation result with errors and warnings

      const validation = await client.validateParams('openai/gpt-3.5-turbo', {
      messages: [{ role: 'user', content: 'Hello' }],
      stream: true,
      max_tokens: 5000
      });

      if (!validation.valid) {
      console.error('Errors:', validation.errors);
      }
      if (validation.warnings?.length) {
      console.warn('Warnings:', validation.warnings);
      }
    • Intelligently truncates a list of messages to respect a token limit Always preserves the system message if it exists, and truncates from the beginning (FIFO)

      Parameters

      • messages: ChatMessage[]

        The messages to truncate

      • maxTokens: number

        Maximum token limit (approximate, based on 4 chars ≈ 1 token)

      Returns ChatMessage[]

      The truncated messages

      const longConversation = [
      { role: 'system', content: 'You are helpful' },
      // ... 50 messages
      ];

      const truncated = client.truncateMessages(longConversation, 4000);
      // Keeps the system message + the most recent messages that fit within 4000 tokens

    Other

    • Sends a chat completion request with streaming to the OpenRouter API.

      Parameters

      • params: ChatCompletionParams

        Complete request parameters (model, messages, and all optional parameters).

      Returns Promise<ReadableStream<any>>

      Response stream.

    • Sends a streaming chat completion request and returns an AsyncIterable. This method provides a more ergonomic API than createChatCompletionStream by converting the ReadableStream to AsyncIterable, allowing the use of for await...of.

      Parameters

      • params: ChatCompletionParams

        Complete request parameters (model, messages, and all optional parameters).

      Returns AsyncIterable<ChatCompletionChunk>

      AsyncIterable of response chunks.

      for await (const chunk of client.streamChatCompletion({
      model: 'openai/gpt-3.5-turbo',
      messages: [{ role: 'user', content: 'Hello!' }]
      })) {
      console.log(chunk.choices[0]?.delta?.content || '');
      }
    • Counts approximately the number of tokens in a text

      Parameters

      • text: string

        Text to analyze

      Returns number

      Approximate number of tokens

    • Counts tokens in an array of messages

      Parameters

      Returns number

      Total approximate number of tokens

    • Retrieves information for a specific model

      Parameters

      • modelId: string

        Model ID (format: "author/name")

      Returns Promise<ModelInfo>

      Model information

    • Retrieves available endpoints for a specific model

      Parameters

      • modelId: string

        Model ID (format: "author/name")

      Returns Promise<ModelEndpoint[]>

      List of model endpoints

    • Retrieves complete model capabilities (supported parameters, modalities, limits) Combines data from getModel() and getModelEndpoints() for an overview

      Parameters

      • modelId: string

        Model ID (e.g: 'anthropic/claude-3.5-sonnet')

      Returns Promise<ModelCapabilities>

      Detailed model capabilities

      const caps = await client.getModelCapabilities('anthropic/claude-3.5-sonnet');
      if (caps.supportsTools) {
      // Use function calling
      }
      if (caps.supportsVision) {
      // Send images
      }
    • Checks if an API key is valid

      Returns Promise<boolean>

      True if the key is valid

    • Retrieves statistics for a specific generation

      Parameters

      • generationId: string

        The generation ID

      Returns Promise<GenerationStats>

      The generation statistics

    • Retrieves detailed information for the API key (usage, limits, rate limits) Endpoint: GET /auth/key

      Returns Promise<KeyInfo>

      Information about the API key

      const keyInfo = await client.getKeyInfo();
      console.log('Usage:', keyInfo.usage);
      console.log('Limit:', keyInfo.limit || 'Unlimited');
      if (keyInfo.rate_limit) {
      console.log('Rate limit:', keyInfo.rate_limit.requests, 'per', keyInfo.rate_limit.interval);
      }
    • Creates a message of type 'tool' to respond to a tool call

      Parameters

      • toolCallId: string

        Tool call ID

      • content: string

        Response content (can be a JSON stringified)

      • Optionalname: string

        Function name (optional)

      Returns ChatMessage

      Formatted message for the API

    • Helper to create a tool response message from any object result. Automatically serializes objects to JSON string.

      Parameters

      • toolCallId: string

        The ID of the tool call this is responding to

      • result: any

        The result object or string to include as content

      • Optionalname: string

        Optional name of the tool function

      Returns ChatMessage

      A properly formatted ChatMessage for tool responses

      const weatherResult = { location: 'Paris', temperature: 22, conditions: 'Sunny' };
      const message = OpenRouterClient.createToolResponseFromResult(
      'call_123',
      weatherResult,
      'get_weather'
      );
    • Marks a message as cacheable for Anthropic Prompt Caching. Adds a "cache breakpoint" allowing to cache all prompt content up to this point.

      Savings: 90% reduction on cached tokens (hit), 10% surcharge (miss). Ideal for: long system prompt, repeated context, multi-turn conversations.

      ⚠️ Note: OpenRouter does not yet return cache metrics (cache_read_input_tokens, cache_creation_input_tokens) even though caching works on Anthropic's side. You will save money but won't see detailed cache metrics in API responses.

      Parameters

      Returns ChatMessage

      New message with cache_control added

      const systemPrompt = OpenRouterClient.markMessageAsCacheable({
      role: 'system',
      content: 'Long system prompt...'
      });
    • Helper to execute tool calls and create response messages

      Parameters

      • toolCalls: ToolCall[]

        Tool calls to execute

      • functions: Record<string, (...args: any[]) => any | Promise<any>>

        Map of available functions

      Returns Promise<ChatMessage[]>

      Formatted response messages