How to beginner · 3 min read

How to get Cerebras API key

Quick answer
To get a Cerebras API key, sign up for an account on the official Cerebras Cloud platform and generate your API key from the dashboard. Use this key as api_key in your Python client to authenticate requests.

PREREQUISITES

  • Python 3.8+
  • pip install openai>=1.0
  • Cerebras Cloud account

Setup

First, create a Cerebras Cloud account at the official website. After logging in, navigate to the API keys section in your user dashboard. Generate a new API key and copy it securely. Then, install the openai Python package if you haven't already:

bash
pip install openai>=1.0
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl (xx kB)
Installing collected packages: openai
Successfully installed openai-1.x.x

Step by step

Use the openai Python SDK with your Cerebras API key and the Cerebras API base URL to authenticate and call the API. Below is a complete example that sends a chat completion request to the Cerebras LLM.

python
import os
from openai import OpenAI

# Set your Cerebras API key in environment variable CEREBRAS_API_KEY
client = OpenAI(
    api_key=os.environ["CEREBRAS_API_KEY"],
    base_url="https://api.cerebras.ai/v1"
)

response = client.chat.completions.create(
    model="llama3.3-70b",
    messages=[{"role": "user", "content": "Hello, Cerebras!"}]
)

print(response.choices[0].message.content)
output
Hello, Cerebras! How can I assist you today?

Common variations

You can use different Cerebras models such as llama3.1-8b by changing the model parameter. The openai SDK supports streaming responses by setting stream=True in the request. For asynchronous usage, use an async client pattern with asyncio.

python
import os
import asyncio
from openai import OpenAI

async def main():
    client = OpenAI(
        api_key=os.environ["CEREBRAS_API_KEY"],
        base_url="https://api.cerebras.ai/v1"
    )

    stream = await client.chat.completions.acreate(
        model="llama3.1-8b",
        messages=[{"role": "user", "content": "Stream a response."}],
        stream=True
    )

    async for chunk in stream:
        print(chunk.choices[0].delta.content or "", end="", flush=True)

asyncio.run(main())
output
Streaming response text appears here in real time...

Troubleshooting

  • If you get an authentication error, verify your CEREBRAS_API_KEY environment variable is set correctly.
  • If the API endpoint is unreachable, check your network and ensure you use the correct base URL https://api.cerebras.ai/v1.
  • For model not found errors, confirm you are using a valid Cerebras model name like llama3.3-70b.

Key Takeaways

  • Sign up on Cerebras Cloud to generate your API key from the dashboard.
  • Use the official OpenAI Python SDK with the Cerebras base URL and your API key.
  • Set your API key securely in the environment variable CEREBRAS_API_KEY.
  • You can switch models or enable streaming by adjusting parameters in the SDK calls.
  • Check environment variables and base URL if you encounter authentication or connectivity issues.
Verified 2026-04 · llama3.3-70b, llama3.1-8b
Verify ↗