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:

Python ask.py
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)
Output
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).

Note: the model has no memory between separate API calls. If you want a multi-turn conversation, you are responsible for collecting each previous user message and assistant reply and re-sending the whole growing list every time — leave an earlier turn out, and the model behaves as if it never happened.