Free modelsExplainedDaily archiveLive dashboard
LLM API guide

How to Use an LLM API in Python: Claude, OpenAI, Gemini

Copy-paste Python to call Claude, OpenAI, Gemini, Grok and DeepSeek, taken from each provider's own quickstart, plus how to see what each call cost.

Before you start

You need Python 3, an API key (how to get one) saved as an environment variable, and the provider's library. Each example sends one prompt and prints the answer.

Claude (Anthropic)

pip install anthropic
import anthropic

client = anthropic.Anthropic()  # reads ANTHROPIC_API_KEY

message = client.messages.create(
    model="claude-opus-5-5",
    max_tokens=1000,
    messages=[{"role": "user", "content": "Explain APIs in one sentence."}],
)

for block in message.content:
    if block.type == "text":
        print(block.text)
print(message.usage)  # input_tokens and output_tokens

OpenAI

pip install openai
from openai import OpenAI

client = OpenAI()  # reads OPENAI_API_KEY

response = client.responses.create(
    model="gpt-6.2-astra",
    input="Explain APIs in one sentence.",
)

print(response.output_text)
print(response.usage)  # input_tokens, output_tokens, total_tokens

Gemini (Google)

pip install -U google-genai
from google import genai

client = genai.Client()  # reads GEMINI_API_KEY

interaction = client.interactions.create(
    model="gemini-4-hydrogen",
    input="Explain APIs in one sentence.",
)
print(interaction.output_text)

Grok, DeepSeek and OpenRouter (OpenAI-compatible)

These accept the OpenAI library: change base_url, the key and the model.

from openai import OpenAI

client = OpenAI(
    api_key="<YOUR_XAI_API_KEY_HERE>",
    base_url="https://api.x.ai/v1",          # DeepSeek: https://api.deepseek.com
)

response = client.responses.create(
    model="grok-4.7",                          # DeepSeek: deepseek-flash
    input="Explain APIs in one sentence.",
)

print(response.output_text)

OpenRouter uses https://openrouter.ai/api/v1 with the Chat Completions format and model IDs like inclusionai/ling-3.0-tiny.

Code from each provider's quickstart, checked 22 November 2026. Model names change often; check the provider's model list if one stops working.

What did that call cost?

Every response reports its tokens. Multiply by the model's price per million: one call with 20 tokens in and 60 out on Claude Fable 5.2 costs $0.0026, and a busy app is just that times many requests. Try your own numbers in the LLM API cost calculator.

Common errors

Next: compare prices

Ready to pick a model? See the LLM API pricing table for all 360 models, the cheapest LLM APIs, and the cost calculator. By lab: Claude, OpenAI, Gemini, DeepSeek, Grok, Mistral, Qwen, Kimi, Meta Llama.

Questions

How do I use an LLM API in Python?

Install the provider's library (pip install anthropic, openai or google-genai), set your API key as an environment variable, create a client and call its create method with a model and your prompt.

Can I use the OpenAI library for other models?

Yes, for providers with OpenAI-compatible APIs such as Grok, DeepSeek and OpenRouter: set base_url and use their key and model names.

How do I use an LLM API for free in Python?

Use a free tier: a Gemini key from Google AI Studio, or OpenRouter's free models with the OpenAI library. See the free LLM API guide.

How do I see how many tokens a call used?

Read the usage field on the response; it lists input and output tokens, which is what you're billed for.

Get this in your inbox at 7am

One free email each morning with the AI repos, models and stories that are picking up speed, including new free models. Your own time zone, no spam.

See today's email