Urgent.News

One page, thousands of outlets. See who else covered it.

Editions

AI

Cómo integrar un LLM (Claude o GPT) en tu aplicación Python

Integrar un modelo de lenguaje (LLM) en una aplicación Python es hoy más sencillo de lo que parece, y abre la puerta a chatbots, asistentes internos, extracción de datos y automatización con lenguaje natural. En esta guía verás el patrón completo, con código real. 1. Elige el proveedor Los tres más usados son Anthropic (Claude) , OpenAI (GPT) y Google (Gemini) . Todos exponen una API HTTP con un…

Integrating a Language Model (LLM) into a Python application has become much simpler, enabling the creation of chatbots, internal assistants, data extraction, and natural language automation. This guide walks through the entire process with real code examples.

1. Choose a Provider

The three most popular providers are Anthropic (Claude), OpenAI (GPT), and Google (Gemini). All of them offer an HTTP API with an official Python SDK, and the logic of your application only changes slightly between them. In this example, Claude will be used, but the pattern is identical for the others.

2. Minimal Call

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

client = Anthropic()

resp = client.messages.create(

model="claude-opus-4-8",

max_tokens=1024,

messages=[

{

"role": "user",

"content": "Summarize photosynthesis in one sentence."

}

],

)

print(resp.content[0].text)

Key points to remember: 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 Better Experience

When waiting for all text to be generated feels slow in an interface. Streaming shows the response token by token, similar to 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)

Streaming large outputs also prevents the request from exceeding the connection's timeout.

4. Structured Output (Reliable JSON)

If you need the model to return data in a specific format (for example, to store it in a database), request a JSON schema instead of parsing free-form text. This eliminates the most common class of bugs when integrating AI: unpredictable answer formats.

5. Production Best Practices

- Control costs by using reasonable max_tokens and caching repetitive answers. Modern SDKs support prompt caching, which saves a lot of context reuse.

- Handle errors and retries: APIs fail; the official SDKs already retry 429 and 5xx errors with exponential backoff. Capture typed exceptions (RateLimitError, etc.) rather than comparing text strings.

- Protect your keys: use environment variables, rotate them periodically, and provide minimal permissions.

Select the model based on the task: a larger model for complex reasoning, or a smaller, faster one for high-volume classification.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in AI

OpenAI Announces Massive Data Center In Ohio With $105 Billion Nvidia Guarantee

OpenAI has signed a 10-year lease for an enormous Ohio data center that will eventually provide 8 gigawatts of computing capacity and require at least 10 gigawatts of new power generation. According to OpenAI, Nvidia will be supplying the chips and guaranteeing up to $105 billion in lease and power obligations.

  • OpenAI secures 10-year data center lease in Ohio
  • Nvidia guarantees $105 billion for lease and power
  • Project funded by Japan under 2025 trade agreement

More from Monday 17 August →