The Agentic Loop Explained
Every agent follows this cycle:
1. Receive Task
↓
Claude Evaluates
"What do I need to do? What tools should I use?"
↓
Tool Calls
Claude requests to use tools (Read, Bash, WebSearch, etc)
↓
Execute Tools
System runs the tools and captures results
↓
Tool Results
Results are sent back to Claude
↓
Claude Decides
"I have the information. Should I use more tools or respond?"
↓
→ If more tools needed: Go to step 3
→ If done: Proceed to step 7
↓
Return Final Response
Claude produces text response with no tool calls
One complete cycle (steps 2-5) is called a turn. The loop continues for multiple turns until Claude decides it has enough information to respond.
Example: Reading a File
Agent task: "Read package.json and tell me what version of React is installed"
Turn 1:
- Claude sees task
- Claude says: "I need to read package.json"
- Agent calls Read("package.json")
- System returns file contents
- Claude reads the result
- Claude says: "React version is 18.2.0"
- No more tools needed, done!
Example: Multi-Turn Task
Agent task: "Find and fix the memory leak in api.ts"
Turn 1:
- Claude: "I need to see the code"
- Agent calls Read("api.ts")
- System returns code
- Claude: "I see some event listeners. Let me check what's listening"
Turn 2:
- Claude: "I need to search for all addEventListener calls"
- Agent calls Grep for "addEventListener"
- System returns matches
- Claude: "Found the issue! Missing removeEventListener"
Turn 3:
- Claude: "I'll fix it"
- Agent calls Edit() with the fix
- Claude: "Fixed! Let me verify with tests"
Turn 4:
- Claude: "Running tests to confirm"
- Agent calls Bash("npm test")
- System returns test results
- Claude: "Tests pass! Done."
---
Three Execution Environments
1. Claude Code CLI (Command Line)
The Claude Code CLI is an interactive assistant that uses agents internally. When you run claude in your terminal, you're using agents.
Key features:
- Spawns subagents automatically when needed
Best for: Day-to-day coding tasks, one-off problems, interactive work
Example:
$ claude "Find all console.log statements and replace them with logger.info"
2. Agent SDK / API
The Agent SDK is a library (Python or TypeScript) you embed in your own code. You write a program that uses agents to solve problems.
Key features:
Best for: Automated workflows, server-side processing, custom applications
Example (Python):
from claudeagentsdk import query
async def main():
async for message in query(
prompt="Analyze these logs and find errors",
options={"allowed_tools": ["Read", "Grep", "WebSearch"]}
):
print(message)
asyncio.run(main())
3. Managed Agents
Anthropic's cloud-hosted managed agents (coming soon). You describe a task and agents run it in the cloud without you managing infrastructure.
---
Why Use Agents?
Problems Agents Solve
Problem 1: Multi-step workflows
Problem 2: Autonomous operation
Problem 3: Complex analysis
When NOT to Use Agents
---
Key Concepts Summary
| Concept | Meaning | |
|---|---|---|
| --------- | ||
| Agent | AI system that decides what tools to use to complete a task | |
| Agentic Loop | Cycle of: decide → use tools → evaluate results → repeat | |
| Turn | One cycle of Claude evaluating and tool execution | |
| Tool | An action the agent can take (Read files, run Bash, etc.) | |
| Tool Result | The output from a tool, sent back to Claude | |
| Context | All conversation history and results accumulated so far |
---
Common Gotchas
Gotcha #1: Agents are slower than one-shot API calls
Gotcha #2: Agents use more tokens
Gotcha #3: Agents aren't perfect
---
Next Steps
You now understand what agents are and how the agentic loop works. In the next module, we'll explore Claude Code's CLI agent system — how to spawn subagents, control them, and use them in your workflow.