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
- Control the cost: set a reasonable
max_tokensand cache repeated responses. Modern SDKs support prompt caching, which greatly cheapens reused context. - Handle errors and retries: APIs fail; the official SDKs already retry 429 and 5xx errors with exponential backoff. Catch the typed exceptions (
RateLimitError, etc.) instead of comparing text strings. - Protect your keys: environment variables, periodic rotation, and minimal permissions.
- Choose the model according to the task: a large model for complex reasoning, a smaller and faster one for high-volume classification.
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.
Xiliux