The model
Reads messages. Returns the next message. It does not open files, run commands, or write to a database.
messages
text or tool_call
run the tool
append the result
An agent is a loop: call the model, run any tool call, append the result, call again. The model never sees files, networks, or databases. It sees the observation your program writes back into messages.
while True:
Pass messages, and the tool schemas, into the API.
completions.create(messages, tools)
You get text or a tool call. The model does not run the tool.
content or tool_calls
Your code runs the function. That is the only side effect.
functions[name](**args)
Append the tool result to messages. The next call can read it. Then call the model again, or stop if there was no tool call.
messages.append(tool_result)
Append the result. Call again. Stop when the response is text.
Reads messages. Returns the next message. It does not open files, run commands, or write to a database.
Executes tools. Appends each result. That append is the observation. Then it calls the model again.