Calling LLMs from Python

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:

  • :robot: Batch processing — analyse hundreds of items automatically
  • :recycle: Custom workflows — chain multiple AI calls together
  • :desktop_computer: Local integration — connect LLMs to your own data and tools
  • :zap: Automation — build pipelines that run without human intervention

The Two Main Providers (and a Third Way)

OpenAI

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

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.

OpenRouter — The Unified Gateway

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 :x: :white_check_mark:
Compare models with same code :x: (different SDKs) :white_check_mark:
Switch models by changing one string :x: :white_check_mark:
Access open-weight models (Llama, Mistral) :x: :white_check_mark:
Pay-per-token, no subscription Varies :white_check_mark:

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

:arrow_right: Read more in the OpenRouter Python SDK documentation.

:bulb: 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.


What You’ll Need

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

:arrow_right: 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.


Next submodule: