Skip to main content
Albero Academy

Building with LLM APIs

Call a model from code, hold a conversation, use tools/function-calling to build agents, and ship responsibly.

Chapter 4Generative AI16 min readBeginner friendly
  • #GenAI
  • #API
  • #Agents

Everything so far comes together in code. Modern LLM providers expose a simple messages API: you send a list of messages, you get a reply. On top of that sit two big capabilities — multi-turn conversations and tool use, which is what turns a chatbot into an agent that can act.

Calling the API

The shape is the same across providers: a model, an optional system prompt, and a list of messages. (When you build for production, reach for the latest, most capable Claude models.)

a first API call
# Pattern common to modern LLM SDKs
response = client.messages.create(
model="claude-sonnet-5",
system="You are Albero's helpful course advisor.",
max_tokens=500,
messages=[
{"role": "user", "content": "Which programme suits a beginner?"}
],
)
print(response.content)

Conversations

The API is stateless — to hold a conversation you resend the whole message history each turn, appending the model's replies and the user's new messages:

multi-turn
messages = [
{"role": "user", "content": "I know Excel. What next?"},
{"role": "assistant", "content": "SQL is the natural next step…"},
{"role": "user", "content": "How long does that take?"}, # context preserved
]

Tools & agents

Tool use (function calling) lets the model call your code — look up a price, query a database, send an email. You describe the tools; the model decides when to use one and returns the arguments; your code runs it and feeds the result back. Chain that loop and you have an agent:

  • Define tools (name, description, input schema).
  • The model, given a task, requests a tool call with arguments.
  • Your code executes it and returns the result.
  • The model uses the result to answer — or calls another tool.

Building responsibly

  • Validate inputs & outputs — never run model-generated code or SQL unchecked.
  • Keep a human in the loop for consequential actions (payments, deletions, emails).
  • Handle failure — models err, time out and rate-limit; degrade gracefully.
  • Protect privacy — be deliberate about what user data you send to a third-party model.

Practice

Design (on paper or in code) a course-advisor agent: a system prompt defining its role, a lookup_programme(name) tool it can call, and the conversation loop. List the guardrails you'd add before letting it email a prospective student.

Key takeaways

  1. 01Modern LLM APIs take a system prompt + messages and return a reply — same shape across providers.
  2. 02For production apps, default to the latest, most capable Claude models.
  3. 03The API is stateless — resend the message history to hold a conversation.
  4. 04Tool use / function calling lets the model call your code; chaining that loop makes an agent.
  5. 05Ship responsibly: validate I/O, keep a human in the loop for high-stakes actions, handle failure, protect data.