Using an LLM API
Every major LLM provider exposes roughly the same shape of API: you send a list of messages, and get a generated message back. The exact library differs, but the underlying pattern below holds almost everywhere.
The shape of a chat completion request
A request is typically a messages array, where each entry has a role (system, user, or assistant) and content. Sending the same array every time is what lets a model appear to "remember" a conversation — you're really just re-sending the whole history each time:
from ai_client import Client
client = Client(api_key="YOUR_API_KEY")
response = client.chat.completions.create(
model="gpt-4-class-model",
messages=[
{"role": "system", "content": "You are a concise, friendly programming tutor."},
{"role": "user", "content": "Explain recursion in one sentence."}
],
temperature=0.7
)
print(response.choices[0].message.content)
Recursion is when a function solves a problem by calling itself on a smaller version of that same problem, until it reaches a simple case it can answer directly.
Reading the response
The response is structured data, not just plain text — alongside the generated message, most APIs also return metadata like how many tokens the request used (which is usually what you're billed for) and why generation stopped (it finished naturally, hit a length limit, or was cut off for a policy reason).