AI Engineering·How-To

Get LLM API Keys (OpenRouter, etc.)

A consolidated guide to obtaining API keys for OpenRouter, Together AI, and OpenAI to build versatile AI applications.


Building AI applications requires access to Large Language Models (LLMs). While Groq is an excellent option for fast inference, having alternatives is crucial for exploring different models, managing rate limits, and ensuring redundancy. This guide covers how to get API keys from three popular providers and use them via the standard openai Python SDK.

1. OpenRouter (The Unified API)

OpenRouter is an aggregator that gives you access to dozens of models (OpenAI, Anthropic, Meta, Mistral, etc.) through a single API key and standardized interface.

How to get the key:

  1. Go to openrouter.ai and sign in.
  2. Navigate to the Keys section from the top navigation or your profile dropdown.
  3. Click Create Key and give it a descriptive name.
  4. Copy the key immediately and save it somewhere safe (like a .env file).

Python Usage:

from openai import OpenAI
import os

client = OpenAI(
  base_url="https://openrouter.ai/api/v1",
  api_key=os.environ.get("OPENROUTER_API_KEY"),
)

completion = client.chat.completions.create(
  model="meta-llama/llama-3.1-8b-instruct",
  messages=[
    {"role": "user", "content": "Explain embeddings in one sentence."}
  ]
)

print(completion.choices[0].message.content)

2. Together AI (High-Performance Open Models)

Together AI provides fast, cost-effective inference for a wide range of open-source models like Llama 3, Mixtral, and Qwen.

How to get the key:

  1. Go to together.ai and sign up for an account.
  2. Once logged in, go to the Settings -> API Keys page.
  3. You will see your default API key, or you can generate a new one.
  4. Copy the key. Together AI often provides free credits to start.

Python Usage:

from openai import OpenAI
import os

client = OpenAI(
  base_url="https://api.together.xyz/v1",
  api_key=os.environ.get("TOGETHER_API_KEY"),
)

completion = client.chat.completions.create(
  model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
  messages=[
    {"role": "user", "content": "What is vector search?"}
  ]
)

print(completion.choices[0].message.content)

3. OpenAI (The Industry Standard)

OpenAI remains the gold standard for foundation models with GPT-4o and its variants.

How to get the key:

  1. Go to the OpenAI Developer Platform and log in.
  2. Navigate to the API Keys section in the dashboard sidebar.
  3. Click Create new secret key.
  4. Copy the key. Note that you may need to add a minimum balance ($5) to start using the API.

Python Usage:

from openai import OpenAI
import os

# No base_url needed, it defaults to OpenAI's endpoint
client = OpenAI(
  api_key=os.environ.get("OPENAI_API_KEY"),
)

completion = client.chat.completions.create(
  model="gpt-4o",
  messages=[
    {"role": "user", "content": "How do LLMs work internally?"}
  ]
)

print(completion.choices[0].message.content)

Important Security Tip

Never hardcode your API keys directly into your Python scripts or commit them to version control (like GitHub). Always use environment variables (e.g., a .env file with the python-dotenv library) to load your keys securely.

Chat with us