Skip to main content
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
  • Python 3.11 or higher - LangChain requires Python 3.11 or higher
  • Basic familiarity with LangChain - Visit LangChain documentation to learn more

Configure LangChain with Cerebras

1

Install required dependencies

Install the LangChain Cerebras integration package. This package provides native LangChain integration for Cerebras models, including chat models and embeddings.
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.
Python:
JavaScript:
2

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.
Alternatively, you can set the environment variable in your shell:
3

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

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

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

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.

Advanced Usage

Using Different Models

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

Building a RAG Application

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

Async Operations

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

Using with LangChain Agents

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

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:

Troubleshooting

Make sure your CEREBRAS_API_KEY environment variable is set correctly. You can verify it’s loaded by running:
If it returns None, your environment variable isn’t set. Try setting it directly in your code for testing:
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:
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.
Yes! LangSmith provides powerful debugging and monitoring capabilities for LangChain applications.
Visit LangSmith to view your traces and debug your applications.
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.

Next Steps

  • Explore LangChain Documentation - Visit the official LangChain docs to learn about chains, agents, and more
  • Try Different Cerebras Models - Experiment with our available 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 for debugging and monitoring your LangChain applications
  • Join the Community - Connect with other developers in the LangChain Discord
  • Read the API Reference - Check out our Chat Completions API documentation for detailed API information
  • Migrate to GLM4.7 - Ready to upgrade? Follow our migration guide to start using our latest model

Additional Resources