Skip to main content
YouRouter provides two primary ways to interact with chat models:
  1. OpenAI-Compatible API: The recommended method for most use cases, providing a unified interface for all models.
  2. Native Provider APIs: For advanced use cases requiring provider-specific features not exposed through the unified API.
For details on how to select providers and models, see the Router Guide.

OpenAI-Compatible API

This is the simplest and most flexible way to use YouRouter. It allows you to use the familiar OpenAI SDKs and switch between different models and providers with minimal code changes.

Basic Usage

The following example shows how to send a basic chat completion request. You can change the model and the vendor header to target different models and providers.

Advanced Features

Multi-turn Conversation

To maintain a continuous conversation, simply pass the entire history of the chat in the messages array.

Streaming Responses

For real-time applications like chatbots, you can stream the response as it’s being generated. Set stream=True in your request.

Function Calling / Tool Use

You can enable models to use tools or call functions to interact with external systems. This is a multi-step process:
  1. You send a request with a list of available tools.
  2. The model responds with a request to call one or more of those tools.
  3. You execute the tools in your code.
  4. You send the tool results back to the model, which then generates a final, natural-language response.

Vision (Multimodal Completions)

Many models support multimodal inputs, allowing you to include images in your requests. This is useful for tasks like image description, analysis, and visual Q&A. This feature is not exclusive to any single provider; models like gpt-4o, claude-3-5-sonnet-20240620, and gemini-1.5-pro-latest all have vision capabilities.

Parameters


Native Provider APIs

For advanced use cases that require parameters or features not available in the OpenAI-compatible API, you can make requests directly to the native provider endpoints. You must include the vendor header in these requests.
YouRouter forwards the entire request body (and all headers except Authorization) to the upstream provider. See the Request Forwarding guide for more details.

Gemini (Google)

Generate Content

Endpoint: POST /v1/projects/cognition/locations/us/publishers/google/models/{model}:generateContent

Safety Settings

You can configure content thresholds by including the safetySettings object in your request. Refer to the official Google AI documentation for a full list of categories and thresholds.

Claude (Anthropic)

Messages API

Endpoint: POST /v1/messages

Tool Use with Claude

You can equip Claude with a set of tools, and it will intelligently decide when to use them to answer a user’s request. This process involves a multi-step conversation where your code executes the tool and sends the result back to Claude. Here’s a complete example demonstrating the full tool-use lifecycle:

Best Practices

  • Routing: For production applications, use the auto routing mode for high availability. For specific model versions or features, use manual routing. See the Router Guide for details.
  • Error Handling: Network issues and provider outages can occur. Implement robust error handling with retries and exponential backoff, especially for long-running tasks.
  • Streaming for UX: For any user-facing application, use streaming to provide a responsive, real-time experience.
  • System Prompts: A well-crafted system prompt is crucial for guiding the model’s behavior, tone, and personality. Test and refine your prompts thoroughly.
  • Token Management: Always be mindful of token limits for both the input context and the output generation. Monitor the usage data returned in the API response to track costs and avoid unexpected truncation.