TechnicalBuild an agent

Build an agentStart here

Build an AI agent from scratch

A loop plus tools. Ten short lessons, one file at a time, until a coding agent is running in the terminal.

3 min readLesson

Build an agentStart

An AI agent is an ordinary program. It talks to an AI model over HTTP, in a loop, and lets the AI ask your program to run small functions called tools.

The AI never touches your computer. It only says “please run read_file with path = routes/web.php”. Your code runs it, sends back the result, and the AI decides what to do next. That is the whole recipe: a loop plus tools.

In ten short lessons you will build a working coding agent in the terminal, one file at a time. Every lesson ends with a check so you know it works before moving on.

  • 10 lessons plus setup
  • About 4 hours at your own pace
  • Needs: PHP, Laravel basics, an OpenAI key

The idea you will build, in six steps

  1. You type a message. It is added to the history array.
  2. Send the history to the AI. One HTTP POST, with the list of tools it may use.
  3. The AI replies. Either with text (the answer) or with requests to run tools.
  4. Run the tools it asked for. Your PHP runs each one and adds the result to the history.
  5. Go round again. Send the bigger history back, until the AI replies with no tool requests.
  6. Show the answer and wait for you. Back to step 1.

The filled steps 2–5 are the agent’s inner loop. You will write it in Lesson 3.

Course outline

  1. Your first AI response. Send one question to OpenAI from an Artisan command and print the answer. 15 min
  2. Have a conversation. Keep a conversation going so the AI remembers what you said earlier. 10 min
  3. The agent loop. Give the AI tools, and add an inner loop that runs them until the AI has an answer. 30 min
  4. Move tools into classes. Give each tool its own class so adding a tool never means editing the loop. 20 min
  5. Make a revenue tool. Answer “How much did we earn last quarter?” with a tool that only accepts a fixed list of options. 10 min
  6. Agent classes and structured output. Move the agent loop into reusable Agent classes, and force an answer into an exact JSON shape. 35 min
  7. Filesystem tools. Give the agent the tools a coding agent needs: write, edit, find, search and run commands. 25 min
  8. Compacting long conversations. Stop a long conversation getting too big by replacing it with a summary. 25 min
  9. Load a guidelines file. Read a project rules file (like CLAUDE.md or AGENTS.md) into the agent’s instructions. 20 min
  10. Agent memory. Let the agent remember lasting facts about you between sessions with a “remember” tool. 25 min

How to follow the steps

Each step tells you exactly what to do with a file. The label on every code block says how to use it:

  • New file. Create this file at the path shown and paste the code in.
  • Replace file. Delete everything in the existing file and paste this in its place.
  • Edit. Change only the part described in the step.
  • Run. Type this in your terminal, from the project folder.