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.
<!-- OpenRouter Section -->
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:
- Go to openrouter.ai and sign in.
- Navigate to the Keys section from the top navigation or your profile dropdown.
- Click Create Key and give it a descriptive name.
- Copy the key immediately and save it somewhere safe (like a
.envfile).
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](https://www.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:
- Go to together.ai and sign up for an account.
- Once logged in, go to the Settings -> API Keys page.
- You will see your default API key, or you can generate a new one.
- 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](https://platform.openai.com/) remains the gold standard for foundation models with GPT-4o and its variants.
How to get the key:
- Go to the OpenAI Developer Platform and log in.
- Navigate to the API Keys section in the dashboard sidebar.
- Click Create new secret key.
- 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.
