Sub topic 1
What an Agent Is
Prerequisites
This chapter assumes basic Python, JSON, messages, and large language model (LLM) tool calling. An LLM is a model that generates text from input messages. You should also understand that an agent can call functions instead of only returning text.
What an agent is
An agent is an LLM wrapped in a control loop. The loop gives the model:
- Instructions: the agent's role and rules.
- Messages: the current conversation or task state.
- Tools: functions the agent may call.
- A stopping rule: a condition such as a final answer or a turn limit.
The model chooses whether to answer, call a tool, or transfer control to another agent. The application executes that choice and sends the result back to the model.
Tool calling in brief
Tool calling is a structured model response that names a function and supplies arguments. Your program validates the arguments, runs the function, and appends its result to the message history.
def lookup_price(item: str) -> float:
prices = {"book": 12.0, "pen": 2.0}
return prices[item]
request = {"name": "lookup_price", "args": {"item": "book"}}
result = lookup_price(**request["args"])
print(result) # 12.0The model does not run the function itself. Your application remains responsible for execution and permissions.
Why use more than one agent
One agent can handle a small workflow. Splitting work helps when tasks need different instructions, tools, or safety rules. A research agent can gather facts, while a writing agent turns approved facts into prose. Smaller roles can also make evaluation easier.
The trade-off is added coordination. Each agent call can increase cost, latency, and the chance of inconsistent context.
Agents as tools
An agent can be exposed to another agent as an ordinary tool. The caller supplies a focused task, and the worker returns a string result.
def research(task: str) -> str:
return f"Research findings for: {task}"
def write(task: str) -> str:
return f"Draft based on: {task}"
def orchestrate(request: str) -> str:
findings = research(request)
return write(findings)
print(orchestrate("Explain solar panels"))In a real system, research and write would run separate agent loops.
The orchestrator pattern
An orchestrator is a manager agent that keeps control of the main workflow. It decomposes a request, calls specialist agents as tools, checks their results, and combines them. This pattern is useful when the manager must decide which specialists to call and how to merge their outputs.
Exercise
When is an orchestrator preferable to a direct handoff?
Show answer
Use an orchestrator when one manager must retain control, call several specialists, and merge or validate their results.