Skip to main content

What is an Agent?

Understand the essence of AI agents: autonomous systems that perceive the environment, make decisions, and act to achieve goals.

SDK Focus query() for await..of message.type session_id

Definition of an Agent

An AI agent is a system that can make autonomous decisions and execute tasks. Unlike traditional software, an agent does not simply follow a preset instruction sequence; it can:

  • Perceive - Understand the current environment and context
  • Decide - Choose the best action based on goals and constraints
  • Act - Execute the chosen operation and observe results
  • Learn - Adjust strategy from feedback

Agent Loop (Core Cycle)

An agent operates on a core loop, which is the foundation for understanding all agent systems:

while not task_complete:
    observation = perceive(environment)
    action = think(observation, goal, memory)
    result = act(action)
    memory.update(observation, action, result)
Claude Agent SDK agent-loop.ts
import { query } from "@anthropic-ai/claude-agent-sdk";

// The simplest possible agent: query() IS the agent loop
const response = query({
  prompt: "List the files in the current directory",
  options: {
    model: "claude-sonnet-4-5",
    allowedTools: ["Read", "Bash", "Glob"]
  }
});

// Stream messages — each is a step in the loop
for await (const message of response) {
  switch (message.type) {
    case "system":    // Session init
      console.log("Session:", message.session_id);
      break;
    case "assistant":  // Agent reasoning
      console.log("Think:", message.content);
      break;
    case "tool_call":  // Agent acts
      console.log("Act:", message.tool_name);
      break;
    case "tool_result": // Observe result
      console.log("Result:", message.content);
      break;
    case "result":     // Loop complete
      console.log("Done:", message.content);
      break;
  }
}

SDK Insight

With the Claude Agent SDK, query() encapsulates the entire agent loop. The SDK handles the perceive-think-act cycle internally — you stream the results via for await..of and observe each step through message.type.

This loop is called the Perception-Action Loop, a fundamental pattern for intelligent systems.

Agent vs Chatbot

Many people confuse agents with chatbots. Here are the key differences:

Feature Chatbot Agent
Interaction style Q&A Task-oriented
Execution capability Text generation only Can call tools and run code
Autonomy Reactive Proactive planning and execution
Memory Limited to conversation context Long-term memory + working memory

One-shot vs Stateful

A chatbot is a one-shot query() call. An agent captures the session_id from the system init message and uses resume to maintain state across interactions. This simple distinction is the foundation of all agent behavior.

Core Components of an Agent

A complete agent system usually includes the following components:

Real-World Examples

Here are some well-known agent implementations:

  • Claude Code - Anthropic's coding agent that understands code, uses tools, and completes development tasks
  • AutoGPT - Early autonomous agent experiments aiming for fully autonomous task execution
  • Devin - Cognition's software engineering agent
  • OpenClaw - Open-source agent development framework

Next Steps

Now that you understand the basics of agents, you can dive deeper:

Try It: Your First Agent

Run a real agent on your machine and observe the loop in action.

  1. Install: npm i @anthropic-ai/claude-agent-sdk
  2. Create first-agent.ts with the code above
  3. Run: npx tsx first-agent.ts
  4. Observe the message types: system → assistant → tool_call → tool_result → result
  5. Capture the session_id from the system message — you'll need it in P4
Gate: P1 Complete — Run query(), capture session_id, explain the difference between a one-shot call and an agent loop.