Sub topic 1
What an AI Agent Is, and When You Need One
Prerequisites
Assume you can write basic Python, call web APIs, handle JSON, and understand that a large language model (LLM) generates text from prompts.
What an AI Agent Is
An AI agent is a program where an LLM chooses actions, runs those actions through tools, observes the results, and continues until it reaches a goal or a limit. A single LLM call produces one response. A fixed workflow follows predetermined steps. An agent selects its next step from the current situation.
flowchart TD
A[User goal] --> B[LLM chooses next action]
B --> C[Tool runs]
C --> D[Tool result]
D --> B
B --> E[Final answer]The loop adds flexibility, but also adds failure modes. The model can choose the wrong tool, provide invalid arguments, or continue indefinitely. Use an agent when the required steps depend on intermediate results. Use a normal function or workflow when the steps are known in advance.
> callout note > An agent is not defined by how conversational it sounds. It is defined by its ability to select and execute actions during a loop.
LLM Foundations
The LLM receives a context window: the limited amount of text it can consider for one request. Context commonly includes system instructions, conversation history, tool descriptions, and tool results. Text is split into tokens, which are small units used to measure input and output size. Long histories consume context and increase cost.
System Prompts
A system prompt gives the agent durable operating rules. State its role, available tools, output requirements, and safety boundaries. Keep instructions specific. For example: “Check the order status before offering a refund” is more useful than “Be helpful.”
Structured Outputs
Structured output requires the model to return data in a defined shape, often JSON. A schema specifies fields, types, and required values. This lets program code validate the response instead of parsing prose.
import json
response = '{"action": "get_weather", "city": "Oslo"}'
data = json.loads(response)
if data.get("action") == "get_weather" and isinstance(data.get("city"), str):
print(data)
else:
raise ValueError("Invalid action")Tool Calling
Tool calling lets the model request a named function with arguments. Your program validates the request, runs the function, and appends its result to the next model request. Return errors as data when possible, so the model can recover. Always enforce permissions and a maximum number of steps outside the model.
Exercise
When should you prefer a fixed workflow over an agent?
Show answer
Prefer a fixed workflow when the steps are known and predictable. Use an agent when intermediate results must determine the next action.