> ## Documentation Index
> Fetch the complete documentation index at: https://inference-docs.cerebras.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Cerebras Inference on OpenRouter

> Learn how to use Cerebras Inference on OpenRouter with Python, LangChain, and JavaScript.

OpenRouter provides a unified API that gives you access to multiple AI providers, including Cerebras, through a single interface. This means you can use familiar tools and SDKs to access Cerebras's ultra-fast inference without changing your existing code structure.

For a complete list of Cerebras Inference powered models available on OpenRouter, visit the [OpenRouter site](https://openrouter.ai/provider/cerebras?utm_source=cerebras\&utm_campaign=openrouter).

## Prerequisites

Before you begin, ensure you have:

* **OpenRouter API Key** - Create a free account and get your API key at [OpenRouter](https://openrouter.ai/settings/keys?utm_source=cerebras\&utm_campaign=openrouter)
* **Python 3.8 or higher** (for Python examples) or **Node.js 16+** (for JavaScript examples)

## Quick Start

<Steps>
  <Step title="Install Dependencies">
    Choose your preferred method:

    <CodeGroup>
      ```bash Python theme={null}
      pip install requests
      ```

      ```bash JavaScript theme={null}
      npm install @langchain/openai
      ```
    </CodeGroup>
  </Step>

  <Step title="Set Your API Key">
    Create a `.env` file in your project directory:

    ```bash theme={null}
    OPENROUTER_API_KEY=your_openrouter_key_here
    ```

    Or set it as an environment variable:

    ```bash theme={null}
    export OPENROUTER_API_KEY="your_openrouter_key_here"
    ```
  </Step>

  <Step title="Make Your First API Call">
    Choose your preferred method to query GPT-OSS 120B on Cerebras:

    <CodeGroup>
      ```python theme={null}
      import os
      import requests

      # Set your OpenRouter API key
      api_key = os.getenv("OPENROUTER_API_KEY")

      url = "https://openrouter.ai/api/v1/chat/completions"

      headers = {
          "Authorization": f"Bearer {api_key}",
          "Content-Type": "application/json"
      }

      # Define the request payload
      data = {
          "model": "gpt-oss-120b",
          "provider": {
              "only": ["Cerebras"]
          },
          "messages": [
              {"role": "system", "content": "You are a helpful assistant."},
              {"role": "user", "content": "What is the capital of France?"}
          ]
      }

      # Send the POST request
      response = requests.post(url, headers=headers, json=data)

      # Print the response
      print(response.json())
      ```

      ```javascript theme={null}
      import 'dotenv/config';
      import { ChatOpenAI } from "@langchain/openai";
      import { HumanMessage, SystemMessage } from "@langchain/core/messages";

      // Initialize ChatOpenAI with OpenRouter
      const llm = new ChatOpenAI({
        modelName: "gpt-oss-120b",
        apiKey: process.env.OPENROUTER_API_KEY,
        timeout: 10000, // 10 second timeout
        configuration: {
          baseURL: "https://openrouter.ai/api/v1",
          defaultHeaders: {
            "HTTP-Referer": "https://cerebras.ai",
            "X-Title": "Cerebras"
          }
        },
        modelKwargs: {
          provider: {
            order: ["Cerebras"]
          }
        }
      });

      // Create messages
      const messages = [
        new SystemMessage("You are a helpful assistant."),
        new HumanMessage("What is the capital of France?")
      ];

      // Get response
      try {
        const response = await llm.invoke(messages);
        console.log(response.content);
      } catch (error) {
        console.error('Error:', error.message);
        // Note: If you see a 402 error, add credits at https://openrouter.ai/settings/credits
      }
      ```
    </CodeGroup>
  </Step>

  <Step title="Advanced Examples">
    You did it — your first API call is complete! Now, let’s explore how to make your model smarter at handling tasks and more precise in how it formats its responses via structured outputs and tool calling. See the examples below for how to use both.

    <CodeGroup>
      ```python Structured Outputs theme={null}
      import os
      import requests
      import json

      api_key = os.getenv("OPENROUTER_API_KEY")
      url = "https://openrouter.ai/api/v1/chat/completions"

      headers = {
          "Authorization": f"Bearer {api_key}",
          "Content-Type": "application/json"
      }

      # Define the JSON schema for the expected output
      movie_schema = {
          "type": "object",
          "properties": {
              "title": {"type": "string"},
              "director": {"type": "string"},
              "year": {"type": "integer"}
          },
          "required": ["title", "director", "year"],
          "additionalProperties": False
      }

      data = {
          "model": "gpt-oss-120b",
          "provider": {
              "only": ["Cerebras"]
          },
          "messages": [
              {"role": "system", "content": "You are a helpful assistant that generates movie recommendations."},
              {"role": "user", "content": "Suggest a sci-fi movie from the 1990s."}
          ],
          "response_format": {
              "type": "json_schema",
              "json_schema": {
                  "name": "movie_schema",
                  "strict": True,
                  "schema": movie_schema
              }
          }
      }

      # Parse and display the result
      response = requests.post(url, headers=headers, json=data)
      result = response.json()
      if 'choices' in result:
          movie_data = json.loads(result['choices'][0]['message']['content'])
          print(json.dumps(movie_data, indent=2))
      else:
          print(f"Error: {result}")

      ```

      ```python Tool Calls theme={null}
      import os
      import requests
      import json

      api_key = os.getenv("OPENROUTER_API_KEY")

      url = "https://openrouter.ai/api/v1/chat/completions"

      headers = {
          "Authorization": f"Bearer {api_key}",
          "Content-Type": "application/json",
          "HTTP-Referer": "https://cerebras.ai",
          "X-Title": "Cerebras"
      }

      # Define the calculator tool
      tools = [
          {
              "type": "function",
              "function": {
                  "name": "calculator",
                  "description": "Performs mathematical calculations.",
                  "parameters": {
                      "type": "object",
                      "properties": {
                          "expression": {
                              "type": "string",
                              "description": "A mathematical expression to evaluate, e.g., 'sqrt(16)'"
                          }
                      },
                      "required": ["expression"]
                  }
              }
          }
      ]

      # Define the message history
      messages = [
          {"role": "system", "content": "You are a helpful assistant capable of performing mathematical calculations using the calculator tool."},
          {"role": "user", "content": "Is the square root of 16 equal to 4?"},
      ]

      # Define the request payload
      data = {
          "model": "gpt-oss-120b",
          "provider": {
              "only": ["Cerebras"]
          },
          "messages": messages,
          "tools": tools,
          "tool_choice": "auto"
      }

      # Send the POST request
      response = requests.post(url, headers=headers, json=data)

      # Parse the response
      result = response.json()

      # Extract the tool call
      if 'choices' not in result:
          print(f"Error: {result}")
      else:
          tool_calls = result['choices'][0]['message'].get('tool_calls', [])
          if tool_calls:
              for tool_call in tool_calls:
                  function_name = tool_call['function']['name']
                  arguments = json.loads(tool_call['function']['arguments'])
                  expression = arguments.get('expression')

                  # Simulate executing the calculator function
                  try:
                      # WARNING: Using eval can be dangerous. In production, use a safe math parser.
                      calculation_result = eval(expression, {"__builtins__": None}, {"sqrt": lambda x: x ** 0.5})
                      tool_response = f"The result of {expression} is {calculation_result}."
                      
                  except Exception as e:
                      tool_response = f"Error evaluating expression: {e}"

                  # Append the tool's response to the message history
                  messages.append({
                      "role": "tool",
                      "tool_call_id": tool_call['id'],
                      "content": tool_response
                  })

                  # Send the updated message history back to the model
                  data = {
                      "model": "gpt-oss-120b",
                      "provider": {
                          "only": ["Cerebras"]
                      },
                      "messages": messages
                  }

                  response = requests.post(url, headers=headers, json=data)
                  final_result = response.json()
                  assistant_reply = final_result['choices'][0]['message']['content']
                  print(assistant_reply)
          else:
              print("No tool calls were made by the model.")

      ```
    </CodeGroup>
  </Step>
</Steps>

## Available Models

OpenRouter provides access to all Cerebras models:

| Model            | Parameters | Best For                                                         |
| ---------------- | ---------- | ---------------------------------------------------------------- |
| **gpt-oss-120b** | 120B       | Largest model for the most demanding tasks                       |
| **zai-glm-4.7**  | 357B       | Advanced 357B parameter model with strong reasoning capabilities |

Visit the [OpenRouter Cerebras provider page](https://openrouter.ai/provider/cerebras?utm_source=cerebras\&utm_campaign=openrouter) for the complete list of available models.

## FAQ

<Accordion title="What context length can I run?">
  This varies by model. See our [provider page](https://openrouter.ai/provider/cerebras?utm_source=cerebras\&utm_campaign=openrouter) to view max context length for each model.
</Accordion>

<Accordion title="What additional latency can I expect when using Cerebras through OpenRouter?">
  A marginal amount of latency may appear as Cerebras Inference is only available via proxy, which has to be queried after your initial API request.
</Accordion>

<Accordion title="Why do I see “Wrong API Format“ when running the OpenRouter test code?">
  The official OpenRouter inference example uses a multimodal input call, which is not currently supported by Cerebras. To avoid this error, use the code provided in Step 2 of the tutorial above.
</Accordion>
