A chatbot answers questions. An agent takes action: it calls tools, retrieves real data, and uses that data in its response.
Chatbot vs Agent
| Chatbot | Agent | |
|---|---|---|
| Data source | Training only | APIs, files, search |
| Actions | Text only | Tool calls |
| Loops | Single request | Multiple iterations |
| Use case | Q&A | Task automation |
The ReAct Pattern
ReAct = Reasoning + Acting. An agent operates in a loop:
User question
↓
REASON: Claude analyzes — is a tool needed?
↓
ACT: Tool call (get_weather, read_file, ...)
↓
OBSERVE: We receive the tool result
↓
REASON: Claude analyzes the result
↓
(repeat until we have a final answer)
↓
ANSWER: Final text response
A Simple Agent
import anthropic
client = anthropic.Anthropic()
TOOLS = [
{
"name": "get_time",
"description": "Возвращает текущее время",
"input_schema": {"type": "object", "properties": {}}
}
]
def get_time() -> str:
from datetime import datetime
return datetime.now().strftime("%H:%M:%S")
def run_agent(question: str) -> str:
messages = [{"role": "user", "content": question}]
while True:
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=512,
tools=TOOLS,
messages=messages,
)
if response.stop_reason == "end_turn":
return next(b.text for b in response.content if hasattr(b, "text"))
messages.append({"role": "assistant", "content": response.content})
results = []
for block in response.content:
if block.type == "tool_use":
if block.name == "get_time":
result = get_time()
results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": result
})
messages.append({"role": "user", "content": results})
print(run_agent("What time is it right now?"))
Common Agent Tools
- Web search — up-to-date information
- File reading — local documents
- HTTP requests — external APIs
- Code execution — computations
- Database — data storage and retrieval
💬 Comments (0)
No comments yet
Be the first to share your opinion about this article!