Using chatbots like ChatGPT or Claude through their web interfaces is great for conversational tasks. But when you need automation, batch processing, or integration into your own workflows, Python gives you programmatic access to the same models.
This module shows you how to call Large Language Models from Python scripts, opening up possibilities like:
The most widely used provider. Their API gives access to GPT-4o, GPT-4.1, o3, and other
models. Their official Python library (openai) is the de facto standard — many other
providers are compatible with it.
To use OpenAI directly: sign up at platform.openai.com,
generate an API key, install pip install openai, and you’re ready.
Anthropic’s Claude models (Sonnet, Opus) are particularly strong at reasoning and code
generation. They have their own Python SDK (pip install anthropic) with a different
API style. Anthropic requires a separate account and API key from
console.anthropic.com.
We will use OpenRouter. Bookmark their documentation site
OpenRouter is a service that provides a single, unified API to access models from dozens of providers — OpenAI, Anthropic, Google, Meta, DeepSeek, Mistral, and many more.
Why this matters for this module:
| Feature | Direct Provider | OpenRouter |
|---|---|---|
| One API key for all models | ||
| Compare models with same code |
|
|
| Switch models by changing one string | ||
| Access open-weight models (Llama, Mistral) | ||
| Pay-per-token, no subscription | Varies |
OpenRouter is compatible with the OpenAI Python library. This means you can
use the standard openai package, point it to OpenRouter’s URL, and authenticate
with your OpenRouter API key. The same code that calls GPT-4o can call Claude,
Llama, Gemini, or many other models by changing a single string.
OpenRouter also provides its own dedicated, feature-rich Python SDK. This is a good option if you want a client designed specifically for OpenRouter, with sync and async clients and type definitions for requests and responses.
Install the OpenAI-compatible client:
pip install openai
Or install the dedicated OpenRouter SDK:
pip install openrouter
Read more in the
OpenRouter Python SDK documentation.
All examples in this module use OpenRouter because it lets you experiment
with different models without changing your code or managing multiple accounts.
To follow along, sign up at openrouter.ai and generate
an API key.
| Requirement | Details |
|---|---|
| Python 3.8+ | Any recent version |
| Python client | Use either pip install openai or pip install openrouter
|
| OpenRouter account | Free to sign up, pay-per-use |
| OpenRouter API key | From openrouter.ai/keys |
| A few credits | $5 goes a long way with smaller models |
If you prefer to use OpenAI or Anthropic directly, the concepts are the
same — just use their respective base URLs and SDKs. The first example will
show both approaches side by side.