Key terms for this tutorial
Here are the words that matter most in this tutorial. Come back here if a term feels fuzzy.
- language model
- A model that predicts the next token (or fills in missing tokens) in a sequence of text.
- model
- A function with learnable parameters that maps inputs to outputs.
- parameters
- The collection of learnable numbers inside a model.
- agent
- A program that chooses actions, often by calling tools, to accomplish a goal.
- function calling
- A model output that includes a request to run a tool, such as a calculator or API.
- ReAct
- A prompting pattern where a model reasons out loud and then acts by calling tools.
Define tools for the model
A tool is a Python function with a JSON schema. The model outputs a JSON object naming the tool and its arguments. Your code runs the function, then appends the result to the conversation.
import json
def calculator(expression: str) -> str:
try:
return str(eval(expression))
except Exception as e:
return str(e)
tools = {
'calculator': {
'description': 'Evaluate a math expression',
'parameters': {'expression': 'a math expression'}
}
}
print(json.dumps(tools, indent=2))
{
"calculator": {
"description": "Evaluate a math expression",
"parameters": {
"expression": "a math expression"
}
}
}
Line-by-line: tools.py
Read this slowly. Each line does one small, concrete thing.
import json— Import thejsonlibrary so we can use its built-in tools.def calculator(expression: str) -> str:— Define thecalculatorfunction, which describes a step in the data or model pipeline.print(json.dumps(tools, indent=2))— Print a result so we can see what the model learned.
Pop-in: Read the code one block at a time
Industry code imports many libraries. Focus on the sequence: prepare data, define the model, run the training loop, and evaluate. The rest is setup.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Build an AI Agent'. Explain the section 'Define tools for the model' in plain English. Include simple analogies, vocabulary definitions, and a short example. Also walk through the code example in this section step by step.
The ReAct loop
ReAct alternates between reasoning and action. The model thinks out loud, decides which tool to use, observes the result, and repeats until it has a final answer.
def agent_run(question, model_stub):
# A fake LLM stub for illustration
history = f'Question: {question}\n'
if '2 + 2' in question:
return '4'
return 'unknown'
print(agent_run('What is 2 + 2?', None))
4
Line-by-line: react.py
Read this slowly. Each line does one small, concrete thing.
def agent_run(question, model_stub):— Define theagent_runfunction, which describes a step in the data or model pipeline.print(agent_run('What is 2 + 2?', None))— Print a result so we can see what the model learned.
Pop-in: Read the code one block at a time
Industry code imports many libraries. Focus on the sequence: prepare data, define the model, run the training loop, and evaluate. The rest is setup.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Build an AI Agent'. Explain the section 'The ReAct loop' in plain English. Include simple analogies, vocabulary definitions, and a short example. Also walk through the code example in this section step by step.
Check yourself
Why a loop?
Why do agents run in a loop instead of one shot?
Complex questions need multiple tool calls. The loop lets the model observe each result and decide the next step, like a human using a calculator and search engine.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Build an AI Agent'. The practice question 'Why a loop?' asks: Why do agents run in a loop instead of one shot? The answer is: Complex questions need multiple tool calls. The loop lets the model observe each result and decide the next step, like a human using a calculator and search engine.. Explain the concept behind this in simple terms.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Build an AI Agent'. Explain the section 'Check yourself' in plain English. Include simple analogies, vocabulary definitions, and a short example.
Three questions
1. What is a tool in an LLM agent?
Stuck or curious? Explain this question to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Build an AI Agent'. The quiz asks: What is a tool in an LLM agent? The correct answer is: A callable function with a schema that the model can invoke. Explain why that answer is right and why the other choices are wrong, using a simple example.
2. What does ReAct stand for?
Stuck or curious? Explain this question to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Build an AI Agent'. The quiz asks: What does ReAct stand for? The correct answer is: Reasoning and Acting. Explain why that answer is right and why the other choices are wrong, using a simple example.
3. Why parse the model's tool call?
Stuck or curious? Explain this question to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Build an AI Agent'. The quiz asks: Why parse the model's tool call? The correct answer is: To run the named function with the provided arguments. Explain why that answer is right and why the other choices are wrong, using a simple example.
Keep learning
Capstone: put everything together in an end-to-end project.
Part of the CodeHS tutorial library.