PythonIALLMsClaudeAPI

How to integrate an LLM (Claude or GPT) into your Python application

Published on 2026-07-07 · Xiliux

Integrating a language model (LLM) into a Python application is easier today than it looks, and it opens the door to chatbots, internal assistants, data extraction, and natural-language automation. In this guide you'll see the complete pattern, with real code.

1. Choose the provider

The three most used are Anthropic (Claude), OpenAI (GPT), and Google (Gemini). They all expose an HTTP API with an official Python SDK, and your app's logic barely changes between them. In the examples I'll use Claude, but the pattern is identical in the others.

Install the SDK and store your key in an environment variable, never in the code:

pip install anthropic
export ANTHROPIC_API_KEY="your-key"

2. The minimal call

The pattern is always the same: you send a list of messages and receive a response.

from anthropic import Anthropic

client = Anthropic()  # reads ANTHROPIC_API_KEY from the environment

resp = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Summarise in one sentence: photosynthesis..."}
    ],
)
print(resp.content[0].text)

Two important details: resp.content is a list of blocks (check .type before reading .text), and max_tokens limits the length of the response.

3. Streaming for a good experience

In an interface, waiting for all the text to be generated feels slow. Streaming shows the response token by token, as in ChatGPT:

with client.messages.stream(
    model="claude-opus-4-8",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a welcome email."}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

For long outputs, streaming also keeps the request from exceeding the connection timeout.

4. Structured output (reliable JSON)

If you need the model to return data in an exact format (for example to store it in a database), ask for a JSON schema instead of parsing free text. This eliminates the most common class of bugs when integrating AI: responses with an unpredictable format.

5. Best practices for production

Need help?

At Xiliux I integrate LLMs into Python applications from end to end: chatbots, AI automation, and internal assistants. If you have an idea, let's talk.

FAQ

Which LLM provider should I choose?

Anthropic (Claude), OpenAI (GPT), and Google (Gemini) each expose an HTTP API with an official Python SDK, and your app's logic barely changes between them. Choose by price, limits, and which model performs best on your task; since the code is almost identical, switching providers later is cheap.

Where do I store the API key?

In an environment variable, NEVER in the code or the repository. A committed key is a leak of credentials and of money (someone uses it on your account). The SDK reads it from the environment by default.

How do I stream the response?

The SDK exposes a streaming mode that delivers the response in chunks as it is generated, instead of waiting until the end. That is what makes a chat 'type' in real time and greatly improves the perception of latency.

How do I get reliable structured output (JSON)?

Ask the model to respond in a specific schema (JSON with the fields you expect) and validate the response against that schema; if it doesn't fit, retry. That way you turn natural language into data your code uses without parsing by hand.

← More articlesRequest a quote