> ## 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.

# Get Started with LangChain

> Learn how to build powerful LLM applications with Cerebras Inference and LangChain's orchestration framework.

LangChain is a framework for developing applications powered by large language models (LLMs). It provides a standard interface for chains, lots of integrations with other tools, and end-to-end chains for common applications. By combining Cerebras's ultra-fast inference with LangChain's powerful orchestration capabilities, you can build production-ready AI applications with unprecedented speed and flexibility.

## Prerequisites

Before you begin, ensure you have:

* **Cerebras API Key** - Get a free API key [here](https://cloud.cerebras.ai/?utm_source=3pi_langchain\&utm_campaign=integrations)
* **Python 3.11 or higher** - LangChain requires Python 3.11 or higher
* **Basic familiarity with LangChain** - Visit [LangChain documentation](https://python.langchain.com/docs/get_started/introduction?utm_source=cerebras\&utm_campaign=langchain) to learn more

## Configure LangChain with Cerebras

<Steps>
  <Step title="Install required dependencies">
    Install the LangChain Cerebras integration package. This package provides native LangChain integration for Cerebras models, including chat models and embeddings.

    <Note>
      **Dependency resolution:** If you encounter dependency conflicts during installation, try running the install command twice. The first run may update core dependencies, and the second run will resolve any remaining conflicts. This is a known behavior with some package managers when updating to newer versions of `langchain-core`.
    </Note>

    **Python:**

    ```bash theme={null}
    pip install langchain-cerebras langchain
    ```

    **JavaScript:**

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

  <Step title="Configure environment variables">
    Create a `.env` file in your project directory to securely store your API key. This keeps your credentials separate from your code.

    ```bash theme={null}
    CEREBRAS_API_KEY=your-cerebras-api-key-here
    ```

    Alternatively, you can set the environment variable in your shell:

    ```bash theme={null}
    export CEREBRAS_API_KEY="your-cerebras-api-key-here"
    ```
  </Step>

  <Step title="Initialize the Cerebras chat model">
    Import and initialize the Cerebras chat model. The `ChatCerebras` class provides a LangChain-compatible interface that automatically handles connection to Cerebras Cloud and includes proper tracking headers.

    <CodeGroup>
      ```python Python theme={null}
      from langchain_cerebras import ChatCerebras
      import os

      # Initialize the Cerebras chat model
      llm = ChatCerebras(
          model="gpt-oss-120b",
          api_key=os.getenv("CEREBRAS_API_KEY"),
          temperature=0.7,
          max_tokens=1024,
      )
      ```

      ```javascript JavaScript theme={null}
      import { ChatOpenAI } from "@langchain/openai";

      // Initialize the Cerebras chat model using OpenAI-compatible interface
      const llm = new ChatOpenAI({
          model: "gpt-oss-120b",
          openAIApiKey: process.env.CEREBRAS_API_KEY,
          configuration: {
              baseURL: "https://api.cerebras.ai/v1",
              defaultHeaders: {
                  "X-Cerebras-3rd-Party-Integration": "langchain"
              }
          },
          temperature: 0.7,
          maxTokens: 1024,
      });
      ```
    </CodeGroup>
  </Step>

  <Step title="Make your first request">
    Now you can use the model just like any other LangChain chat model. This example demonstrates basic message handling with system and user messages.

    <CodeGroup>
      ```python Python theme={null}
      from langchain_cerebras import ChatCerebras
      from langchain_core.messages import HumanMessage, SystemMessage
      import os

      # Initialize the model
      llm = ChatCerebras(
          model="gpt-oss-120b",
          api_key=os.getenv("CEREBRAS_API_KEY"),
      )

      # Create messages
      messages = [
          SystemMessage(content="You are a helpful AI assistant."),
          HumanMessage(content="What are the key benefits of using Cerebras for AI inference?")
      ]

      # Get response
      response = llm.invoke(messages)
      print(response.content)
      ```

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

      dotenv.config();

      // Initialize the model
      const llm = new ChatOpenAI({
          model: "gpt-oss-120b",
          openAIApiKey: process.env.CEREBRAS_API_KEY,
          configuration: {
              baseURL: "https://api.cerebras.ai/v1",
              defaultHeaders: {
                  "X-Cerebras-3rd-Party-Integration": "langchain"
              }
          },
      });

      // Create messages
      const messages = [
          new SystemMessage("You are a helpful AI assistant."),
          new HumanMessage("What are the key benefits of using Cerebras for AI inference?")
      ];

      // Get response
      const response = await llm.invoke(messages);
      console.log(response.content);
      ```
    </CodeGroup>
  </Step>

  <Step title="Use with LangChain chains">
    LangChain's real power comes from chaining operations together. This example uses LCEL (LangChain Expression Language) to create a composable translation chain.

    <CodeGroup>
      ```python Python theme={null}
      from langchain_cerebras import ChatCerebras
      from langchain_core.prompts import ChatPromptTemplate
      from langchain_core.output_parsers import StrOutputParser
      import os

      # Initialize components
      llm = ChatCerebras(
          model="gpt-oss-120b",
          api_key=os.getenv("CEREBRAS_API_KEY"),
      )

      prompt = ChatPromptTemplate.from_messages([
          ("system", "You are a helpful assistant that translates {input_language} to {output_language}."),
          ("human", "{text}")
      ])

      output_parser = StrOutputParser()

      # Create chain using LCEL
      chain = prompt | llm | output_parser

      # Use the chain
      result = chain.invoke({
          "input_language": "English",
          "output_language": "French",
          "text": "Hello, how are you?"
      })

      print(result)
      ```

      ```javascript JavaScript theme={null}
      import { ChatOpenAI } from "@langchain/openai";
      import { ChatPromptTemplate } from "@langchain/core/prompts";
      import { StringOutputParser } from "@langchain/core/output_parsers";
      import dotenv from 'dotenv';

      dotenv.config();

      // Initialize components
      const llm = new ChatOpenAI({
          model: "gpt-oss-120b",
          openAIApiKey: process.env.CEREBRAS_API_KEY,
          configuration: {
              baseURL: "https://api.cerebras.ai/v1",
              defaultHeaders: {
                  "X-Cerebras-3rd-Party-Integration": "langchain"
              }
          },
      });

      const prompt = ChatPromptTemplate.fromMessages([
          ["system", "You are a helpful assistant that translates {input_language} to {output_language}."],
          ["human", "{text}"]
      ]);

      const outputParser = new StringOutputParser();

      // Create chain using LCEL
      const chain = prompt.pipe(llm).pipe(outputParser);

      // Use the chain
      const result = await chain.invoke({
          input_language: "English",
          output_language: "French",
          text: "Hello, how are you?"
      });

      console.log(result);
      ```
    </CodeGroup>
  </Step>

  <Step title="Enable streaming responses">
    Cerebras models support streaming, which is perfect for real-time applications. Streaming allows you to display responses as they're generated, providing a better user experience.

    <CodeGroup>
      ```python Python theme={null}
      from langchain_cerebras import ChatCerebras
      from langchain_core.messages import HumanMessage
      import os

      # Initialize with streaming enabled
      llm = ChatCerebras(
          model="gpt-oss-120b",
          api_key=os.getenv("CEREBRAS_API_KEY"),
          streaming=True,
      )

      # Stream the response
      for chunk in llm.stream([HumanMessage(content="Write a short poem about AI")]):
          print(chunk.content, end="", flush=True)
      ```

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

      dotenv.config();

      // Initialize with streaming enabled
      const llm = new ChatOpenAI({
          model: "gpt-oss-120b",
          openAIApiKey: process.env.CEREBRAS_API_KEY,
          configuration: {
              baseURL: "https://api.cerebras.ai/v1",
              defaultHeaders: {
                  "X-Cerebras-3rd-Party-Integration": "langchain"
              }
          },
          streaming: true,
      });

      // Stream the response
      const stream = await llm.stream([new HumanMessage("Write a short poem about AI")]);
      for await (const chunk of stream) {
          process.stdout.write(chunk.content);
      }
      ```
    </CodeGroup>
  </Step>
</Steps>

## Advanced Usage

### Using Different Models

Cerebras supports multiple high-performance models. Choose the right model based on your use case:

<CodeGroup>
  ```python Python theme={null}
  from langchain_cerebras import ChatCerebras
  import os

  # Use GPT-OSS 120B for complex reasoning and large-scale tasks
  gpt_oss = ChatCerebras(
      model="gpt-oss-120b",
      api_key=os.getenv("CEREBRAS_API_KEY"),
  )

  # Use Llama 3.1 8B for faster, lighter tasks
  llama_8b = ChatCerebras(
      model="gpt-oss-120b",
      api_key=os.getenv("CEREBRAS_API_KEY"),
  )
  ```

  ```javascript JavaScript theme={null}
  import { ChatOpenAI } from "@langchain/openai";
  import dotenv from 'dotenv';

  dotenv.config();

  const config = {
      openAIApiKey: process.env.CEREBRAS_API_KEY,
      configuration: {
          baseURL: "https://api.cerebras.ai/v1",
          defaultHeaders: {
              "X-Cerebras-3rd-Party-Integration": "langchain"
          }
      },
  };

  // Use GPT-OSS 120B for complex reasoning and large-scale tasks
  const gptOss = new ChatOpenAI({
      ...config,
      model: "gpt-oss-120b",
  });

  // Use Llama 3.1 8B for faster, lighter tasks
  const llama8b = new ChatOpenAI({
      ...config,
      model: "gpt-oss-120b",
  });
  ```
</CodeGroup>

### Building a RAG Application

Here's a complete example of building a Retrieval-Augmented Generation (RAG) application with Cerebras and LangChain:

<CodeGroup>
  ```python Python theme={null}
  from langchain_cerebras import ChatCerebras
  from langchain_core.prompts import ChatPromptTemplate
  from langchain_core.output_parsers import StrOutputParser
  from langchain_core.runnables import RunnablePassthrough
  import os

  # Initialize the model
  llm = ChatCerebras(
      model="gpt-oss-120b",
      api_key=os.getenv("CEREBRAS_API_KEY"),
  )

  # Create a RAG prompt template
  template = """Answer the question based only on the following context:
  {context}

  Question: {question}

  Answer:"""

  prompt = ChatPromptTemplate.from_template(template)

  # Create the RAG chain
  rag_chain = (
      {"context": RunnablePassthrough(), "question": RunnablePassthrough()}
      | prompt
      | llm
      | StrOutputParser()
  )

  # Use the chain
  context = "Cerebras has developed the world's largest and fastest AI processor, the Wafer-Scale Engine-3 (WSE-3)."
  question = "What has Cerebras developed?"

  answer = rag_chain.invoke({"context": context, "question": question})
  print(answer)
  ```

  ```javascript JavaScript theme={null}
  import { ChatOpenAI } from "@langchain/openai";
  import { ChatPromptTemplate } from "@langchain/core/prompts";
  import { StringOutputParser } from "@langchain/core/output_parsers";
  import { RunnablePassthrough, RunnableSequence } from "@langchain/core/runnables";
  import dotenv from 'dotenv';

  dotenv.config();

  // Initialize the model
  const llm = new ChatOpenAI({
      model: "gpt-oss-120b",
      openAIApiKey: process.env.CEREBRAS_API_KEY,
      configuration: {
          baseURL: "https://api.cerebras.ai/v1",
          defaultHeaders: {
              "X-Cerebras-3rd-Party-Integration": "langchain"
          }
      },
  });

  // Create a RAG prompt template
  const template = `Answer the question based only on the following context:
  {context}

  Question: {question}

  Answer:`;

  const prompt = ChatPromptTemplate.fromTemplate(template);

  // Create the RAG chain
  const ragChain = RunnableSequence.from([
      {
          context: new RunnablePassthrough(),
          question: new RunnablePassthrough()
      },
      prompt,
      llm,
      new StringOutputParser()
  ]);

  // Use the chain
  const context = "Cerebras has developed the world's largest and fastest AI processor, the Wafer-Scale Engine-3 (WSE-3).";
  const question = "What has Cerebras developed?";

  const answer = await ragChain.invoke({ context, question });
  console.log(answer);
  ```
</CodeGroup>

### Async Operations

For high-throughput applications, use async operations to handle multiple requests concurrently:

<CodeGroup>
  ```python Python theme={null}
  import asyncio
  from langchain_cerebras import ChatCerebras
  from langchain_core.messages import HumanMessage
  import os

  async def get_response():
      llm = ChatCerebras(
          model="gpt-oss-120b",
          api_key=os.getenv("CEREBRAS_API_KEY"),
      )
      
      response = await llm.ainvoke([HumanMessage(content="Hello!")])
      return response.content

  # Run async function
  result = asyncio.run(get_response())
  print(result)
  ```

  ```javascript JavaScript theme={null}
  import { ChatOpenAI } from "@langchain/openai";
  import { HumanMessage } from "@langchain/core/messages";

  import dotenv from 'dotenv';

  dotenv.config();

  async function getResponse() {
      const llm = new ChatOpenAI({
          model: "gpt-oss-120b",
          openAIApiKey: process.env.CEREBRAS_API_KEY,
          configuration: {
              baseURL: "https://api.cerebras.ai/v1",
              defaultHeaders: {
                  "X-Cerebras-3rd-Party-Integration": "langchain"
              }
          },
      });
      
      const response = await llm.invoke([new HumanMessage("Hello!")]);
      return response.content;
  }

  // Run async function
  const result = await getResponse();
  console.log(result);
  ```
</CodeGroup>

### Using with LangChain Agents

Cerebras models work seamlessly with LangChain agents for building autonomous AI systems:

```python theme={null}
from langchain_cerebras import ChatCerebras
from langchain.agents import create_agent
from langchain_core.tools import tool
import os

# Initialize the model
llm = ChatCerebras(
    model="gpt-oss-120b",
    api_key=os.getenv("CEREBRAS_API_KEY"),
)

# Define tools using the @tool decorator
@tool
def get_word_length(word: str) -> int:
    """Returns the length of a word."""
    return len(word)

tools = [get_word_length]

# Create agent using the simplified create_agent API (LangChain 1.0+)
agent = create_agent(
    llm,
    tools,
    system_prompt="You are a helpful assistant."
)

# Run agent
result = agent.invoke({"messages": [{"role": "user", "content": "How many letters are in the word 'Cerebras'?"}]})
print(result["messages"][-1].content)
```

## Using OpenAI Client Directly

If you prefer to use the OpenAI client directly instead of the LangChain integration, you can configure it to work with Cerebras:

<CodeGroup>
  ```python Python theme={null}
  from openai import OpenAI
  import os

  client = OpenAI(
      api_key=os.getenv("CEREBRAS_API_KEY"),
      base_url="https://api.cerebras.ai/v1",
      default_headers={
          "X-Cerebras-3rd-Party-Integration": "langchain"
      }
  )

  response = client.chat.completions.create(
      model="gpt-oss-120b",
      messages=[
          {"role": "system", "content": "You are a helpful assistant."},
          {"role": "user", "content": "What is the capital of France?"}
      ],
      max_tokens=500,
  )

  print(response.choices[0].message.content)
  ```

  ```javascript JavaScript theme={null}
  import OpenAI from 'openai';
  import dotenv from 'dotenv';

  dotenv.config();

  const client = new OpenAI({
      apiKey: process.env.CEREBRAS_API_KEY,
      baseURL: 'https://api.cerebras.ai/v1',
      defaultHeaders: {
          'X-Cerebras-3rd-Party-Integration': 'langchain'
      }
  });

  const response = await client.chat.completions.create({
      model: 'gpt-oss-120b',
      messages: [
          { role: 'system', content: 'You are a helpful assistant.' },
          { role: 'user', content: 'What is the capital of France?' }
      ],
      max_tokens: 500,
  });

  console.log(response.choices[0].message.content);
  ```

  ```bash cURL theme={null}
  curl https://api.cerebras.ai/v1/chat/completions \
      -H 'Authorization: Bearer YOUR_CEREBRAS_API_KEY' \
      -H 'Content-Type: application/json' \
      -H 'X-Cerebras-3rd-Party-Integration: langchain' \
      -d '{
          "model": "gpt-oss-120b",
          "messages": [
              {
                  "role": "system",
                  "content": "You are a helpful assistant."
              },
              {
                  "role": "user",
                  "content": "What is the capital of France?"
              }
          ],
          "max_tokens": 500
      }'
  ```
</CodeGroup>

## Troubleshooting

<Accordion title="Why am I getting authentication errors?">
  Make sure your `CEREBRAS_API_KEY` environment variable is set correctly. You can verify it's loaded by running:

  ```python theme={null}
  import os
  print(os.getenv("CEREBRAS_API_KEY"))
  ```

  If it returns `None`, your environment variable isn't set. Try setting it directly in your code for testing:

  ```python theme={null}
  from langchain_cerebras import ChatCerebras

  llm = ChatCerebras(
      model="gpt-oss-120b",
      api_key="your-api-key-here",
  )
  ```
</Accordion>

<Accordion title="How do I handle rate limits?">
  Cerebras Cloud has generous rate limits, but if you're making many concurrent requests, consider:

  1. Using async operations with controlled concurrency
  2. Implementing retry logic with exponential backoff
  3. Batching requests when possible

  Example with retry logic:

  ```python theme={null}
  from langchain_cerebras import ChatCerebras
  from tenacity import retry, stop_after_attempt, wait_exponential
  import os

  @retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
  def get_completion(prompt):
      llm = ChatCerebras(
          model="gpt-oss-120b",
          api_key=os.getenv("CEREBRAS_API_KEY"),
      )
      return llm.invoke(prompt)
  ```
</Accordion>

<Accordion title="What's the difference between ChatCerebras and using OpenAI client directly?">
  `ChatCerebras` is a native LangChain integration that:

  1. Provides a consistent interface with other LangChain chat models
  2. Automatically handles message formatting and parsing
  3. Supports all LangChain features like callbacks, streaming, and async
  4. Includes proper integration tracking headers
  5. Works seamlessly with LangChain chains and agents

  If you're building with LangChain, use `ChatCerebras`. If you need direct API access, use the OpenAI client with Cerebras base URL.
</Accordion>

<Accordion title="Can I use Cerebras with LangSmith for tracing?">
  Yes! LangSmith provides powerful debugging and monitoring capabilities for LangChain applications.

  ```python theme={null}
  import os
  from langchain_cerebras import ChatCerebras

  # Enable LangSmith tracing
  os.environ["LANGCHAIN_TRACING_V2"] = "true"

  llm = ChatCerebras(
      model="gpt-oss-120b",
      api_key=os.getenv("CEREBRAS_API_KEY"),
  )

  # All calls will now be traced in LangSmith
  response = llm.invoke("Hello!")
  print(response.content)
  ```

  Visit [LangSmith](https://smith.langchain.com/?utm_source=cerebras\&utm_campaign=langchain) to view your traces and debug your applications.
</Accordion>

<Accordion title="Which Cerebras model should I use?">
  Choose based on your use case:

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

  All models run at blazing-fast speeds on Cerebras hardware. Learn more about [available models](/models).
</Accordion>

## Next Steps

* **Explore LangChain Documentation** - Visit the [official LangChain docs](https://python.langchain.com/docs/get_started/introduction?utm_source=cerebras\&utm_campaign=langchain) to learn about chains, agents, and more
* **Try Different Cerebras Models** - Experiment with our [available models](/models) to find the best fit for your use case
* **Build Complex Chains** - Combine multiple LangChain components to create sophisticated AI workflows
* **Explore LangSmith** - Use [LangSmith](https://smith.langchain.com/?utm_source=cerebras\&utm_campaign=langchain) for debugging and monitoring your LangChain applications
* **Join the Community** - Connect with other developers in the [LangChain Discord](https://discord.gg/langchain?utm_source=cerebras\&utm_campaign=langchain)
* **Read the API Reference** - Check out our [Chat Completions API documentation](/api-reference/chat-completions) for detailed API information
* **Migrate to GLM4.7** - Ready to upgrade? Follow our [migration guide](https://inference-docs.cerebras.ai/resources/glm-47-migration) to start using our latest model

## Additional Resources

* [Cerebras API Reference](/api-reference/chat-completions) - Detailed API documentation
* [LangChain Cerebras Provider](https://python.langchain.com/docs/integrations/providers/cerebras?utm_source=cerebras\&utm_campaign=langchain) - Official LangChain integration docs
* [Cerebras Models](/models) - Learn about available models and their capabilities
* [LangChain Cookbook](https://github.com/langchain-ai/langchain/tree/master/cookbook?utm_source=cerebras\&utm_campaign=langchain) - Example notebooks and recipes
* [LangChain Expression Language (LCEL)](https://python.langchain.com/docs/expression_language?utm_source=cerebras\&utm_campaign=langchain) - Learn about building chains with LCEL
