Building a Simple AI Agent
We have all the pieces: API Keys, System Prompts, Chat History, and Tool Calling. When you put them all together, you are no longer just writing scripts—you are building an AI Agent.
An Agent is a system that observes its environment, makes decisions, and takes action.
The Core Agent Loop
Let's look at the pseudo-code for a basic AI Agent that can help a user manage their calendar.
-
Setup: You define the tools:
get_schedule(date),add_meeting(time, person), andcancel_meeting(id). You define the System Prompt: "You are an executive assistant. Manage the user's calendar. Be polite but brief." You initializechatHistory = []. -
The User Trigger: User says: "Do I have time to meet with Sarah tomorrow afternoon? If so, book it for 2pm."
-
The First API Call (Observation): Your server sends the prompt and tools to Claude. Claude realizes it doesn't know the schedule. Claude responds with a
tool_userequest forget_schedule("tomorrow"). -
The First Action (Execution): Your server intercepts the request and runs
get_schedule("tomorrow"). It finds a meeting from 1pm to 3pm. -
The Second API Call (Decision): Your server appends the result to
chatHistoryand sends it back to Claude. Claude reads the result: Ah, there is a meeting at 2pm. I cannot book Sarah then. Claude does NOT answer the user yet. Instead, Claude decides to take another action based on the new data! Claude responds with a new text block: "You have a conflict at 2pm. Would 3:30pm work instead?" -
The User Loop: The user replies: "Yes, 3:30 is fine."
-
The Final Action: Claude reads the approval and sends a
tool_userequest foradd_meeting("3:30pm", "Sarah"). Your server executes the addition, returns the success message to Claude, and Claude finally tells the user: "Done! Sarah is booked for 3:30."
The Magic of Agency
Notice what happened in step 5. You didn't write an if/else statement in your code saying "If there is a conflict, suggest a new time."
Claude made that logical leap entirely on its own based on the System Prompt. This is the magic of Agentic AI. You provide the tools (the hands) and the rules (the system prompt), and the LLM acts as the central brain, dynamically routing logic and executing tools to achieve the user's goal.