Phase 7: Agentic Cognition & Autonomy [7000]¶
Table of Contents¶
- Overview
- Why Agents Matter
- ReAct Agent Loop Architecture
- Multi-Agent System Architecture
- Tool Calling Pipeline
- Agent Type Comparison
- Framework Comparison
- Tool Registry
- Module Structure
- Learning Path
- Cheat Sheets
- Key Takeaways
- Common Pitfalls
- Pro Tips
- Performance Benchmarks
- Related Experiments
- Prerequisites
- Assessment
- Related Topics
- Quick Reference
Overview¶
Creating autonomous AI systems that can reason, plan, and execute complex tasks.
This phase covers AI agent architectures, tool use, multi-agent orchestration, and memory systems for building autonomous AI systems that can manage your infrastructure and code, enabling you to:
- Build autonomous agents that reason and plan
- Implement tool calling for real-world actions
- Orchestrate multi-agent teams
- Create persistent memory systems
- Deploy production-grade agent systems
Why Agents Matter¶
From Chatbots to Agents¶
┌─────────────────────────────────────────────────────────┐
│ Chatbot (Passive) │
├─────────────────────────────────────────────────────────┤
│ - Single-turn responses │
│ - No memory of past interactions │
│ - Cannot take actions │
│ - No reasoning or planning │
│ - Limited to text generation │
└─────────────────────────────────────────────────────────┘
With Agents:
┌─────────────────────────────────────────────────────────┐
│ AI Agent (Active) │
├─────────────────────────────────────────────────────────┤
│ + Multi-step reasoning and planning │
│ + Persistent memory across sessions │
│ + Can execute tools and APIs │
│ + Autonomous decision making │
│ + Interacts with external systems │
└─────────────────────────────────────────────────────────┘
The Agentic Advantage¶
graph LR
A[Task: Deploy Model to Production] --> B{Approach}
B -->|Manual| C[Developer writes code]
C --> D[Test and deploy]
D --> E[Hours of work]
B -->|Chatbot| F[Ask for help]
F --> G[Get code snippet]
G --> H[Still need to implement]
B -->|Agent| I[Agent automates entire flow]
I --> J[Reason, Plan, Execute]
J --> K[Minutes to complete]
style C fill:#fdd
style F fill:#fed
style I fill:#dfd
Real-World Agent Use Cases¶
| Use Case | Agent Type | Tools Used | Time Saved |
|---|---|---|---|
| DevOps Automation | ReAct + Tools | K8s API, Git, Docker | 4 hours → 5 minutes |
| Code Review | Multi-Agent | Static analysis, LLM | 1 hour → 5 minutes |
| Data Analysis | ReAct + Code Interpreter | Pandas, Matplotlib | 2 hours → 10 minutes |
| Customer Support | Multi-Agent + RAG | Knowledge base, CRM | 15 minutes → 1 minute |
| Research Assistant | ReAct + Search | Web search, Arxiv | 3 hours → 20 minutes |
ReAct Agent Loop Architecture¶
graph TD
A[User Query] --> B[Reasoning Engine]
B --> C{Thought}
C --> D{Action Needed?}
D -->|Yes| E[Tool Selection]
D -->|No| F[Generate Response]
E --> G[Tool Execution]
G --> H[Observation]
H --> I{Task Complete?}
I -->|No| B
I -->|Yes| F
F --> J[Final Response]
J --> K[Memory Store]
style A fill:#e1f5fe
style J fill:#c8e6c9
style C fill:#fff9c4
style G fill:#f8bbd0
ReAct Pattern Breakdown¶
┌──────────────────────────────────────────────────────────────────┐
│ ReAct Loop Components │
├──────────────────────────────────────────────────────────────────┤
│ Component │ Function │
├──────────────────────────────────────────────────────────────────┤
│ Thought │ Reasoning step: "What should I do next?" │
│ Action │ Tool/function call with parameters │
│ Observation │ Result from tool execution │
│ Memory │ Store/retrieve context from past steps │
│ Finish │ Final answer when task is complete │
└──────────────────────────────────────────────────────────────────┘
Multi-Agent System Architecture¶
graph TB
subgraph "User Interface"
A[User Query] --> B[Orchestrator Agent]
end
subgraph "Agent Fleet"
B --> C[Research Agent]
B --> D[Coder Agent]
B --> E[Reviewer Agent]
B --> F[Executor Agent]
end
subgraph "Tool Layer"
C --> G[Search Tools]
D --> H[Code Tools]
E --> I[Analysis Tools]
F --> J[Execution Tools]
end
subgraph "Shared Memory"
K[Vector Store]
L[Knowledge Graph]
M[Working Memory]
end
C --> K
D --> K
E --> L
F --> M
K -.->|Context| B
L -.->|Facts| B
M -.->|History| B
style B fill:#ffcc80
style K fill:#c8e6c9
style L fill:#c8e6c9
style M fill:#c8e6c9
Multi-Agent Patterns¶
| Pattern | Description | Example |
|---|---|---|
| Hierarchical | Manager coordinates worker agents | Orchestrator delegates to specialists |
| Sequential | Pipeline of agents processing data | Research → Write → Review → Publish |
| Parallel | Agents work independently on subtasks | Multiple researchers on different sources |
| Consensus | Agents vote on decisions | Review panel for code approval |
| Debate | Agents critique each other | Devil's advocate for quality |
Tool Calling Pipeline¶
graph LR
subgraph "Agent"
A[Thought Process] --> B[Tool Needed?]
B -->|Yes| C[Tool Selection]
end
subgraph "Tool Registry"
C --> D[Function Schema]
D --> E[Parameters]
end
subgraph "Execution"
E --> F[Sandboxed Runner]
F --> G[Result Capture]
end
subgraph "Observation"
G --> H[Error Handling]
H --> I{Success?}
I -->|Yes| J[Return Result]
I -->|No| K[Error Message]
end
J --> A
K --> A
style A fill:#e1f5fe
style F fill:#fff9c4
style J fill:#c8e6c9
style K fill:#ffcdd2
Agent Type Comparison¶
Single Agent vs Multi-Agent¶
| Aspect | Single Agent | Multi-Agent |
|---|---|---|
| Complexity | Simple | Complex |
| Coordination | None | Required |
| Parallelization | Limited | Yes |
| Specialization | Generalist | Specialist roles |
| Cost | Lower | Higher |
| Best For | Simple tasks | Complex workflows |
| Example | Code assistant | DevOps automation team |
Agent Architectures¶
┌─────────────────────────────────────────────────────────────────┐
│ Agent Architecture Spectrum │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Simple ──────────────────────────────────────────── Complex │
│ │
│ Prompt → LLM Multi-Agent System │
│ ↓ ↓ │
│ Response ┌─────────────────┐ │
│ │ Orchestrator │ │
│ └───────┬─────────┘ │
│ ↓ │
│ ┌───────┴───────┐ │
│ ↓ ↓ │
│ [Agent 1] [Agent 2] │
│ ↓ ↓ │
│ [Tools] [Tools] │
│ ↓ ↓ │
│ └─────────────────┘ │
│ ↓ │
│ [Shared Memory] │
│ │
└─────────────────────────────────────────────────────────────────┘
Framework Comparison¶
Agent Framework Selection Matrix¶
| Framework | Type | Learning Curve | Best For | Ecosystem |
|---|---|---|---|---|
| LangGraph | Multi-Agent | Medium | Complex workflows | Excellent |
| AutoGen | Multi-Agent | Medium | Conversational agents | Good |
| CrewAI | Multi-Agent | Low | Role-playing agents | Medium |
| OpenAI Agents SDK | Multi-Agent | Low | Handoffs + guardrails (Swarm's successor) | Growing |
| LangChain | Single/Multi | High | Production systems | Excellent |
| LlamaIndex Agents | Single/Multi | Medium | RAG-enhanced agents | Good |
Feature Comparison¶
Framework Feature Comparison
| Feature | LangGraph | AutoGen | CrewAI | Agents | LC |
|---|---|---|---|---|---|
| Visual Builder | Y | N | N | N | N |
| Type Safety | Y | ! | N | N | N |
| Persistence | Y | Y | Y | N | Y |
| Human-in-Loop | Y | Y | Y | Y | Y |
| Memory Systems | Y | ! | ! | N | Y |
| Tool Calling | Y | Y | Y | Y | Y |
| Multi-Modal | Y | Y | N | Y | Y |
| Streaming | Y | Y | ! | Y | Y |
| Easy Debug | Y | Y | Y | Y | ! |
| Production Ready | Y | Y | ! | ! | Y |
Recommended by Use Case¶
| Use Case | Recommended Framework | Why |
|---|---|---|
| Complex Workflows | LangGraph | Visual builder, state management |
| Chat Between Agents | AutoGen | Built-in conversational patterns |
| Role-Based Teams | CrewAI | Simple role definition |
| Quick Prototyping | OpenAI Agents SDK | Swarm's successor (archived): minimal setup, handoffs + guardrails |
| Production Systems | LangChain | Mature ecosystem, battle-tested |
| RAG + Agents | LlamaIndex | Native RAG integration |
Tool Registry¶
Essential Agent Tools¶
# Infrastructure Management Tools
tools_local = {
# GPU/Compute Management
"nvidia_smi": {
"description": "Get GPU utilization and memory stats",
"parameters": {"gpu_id": "int (optional)"},
"returns": "GPU utilization, memory, temperature"
},
"run_inference": {
"description": "Run LLM inference on specific model",
"parameters": {
"model": "str (model path)",
"prompt": "str (input prompt)",
"max_tokens": "int"
},
"returns": "Generated text and metadata"
},
"quantize_model": {
"description": "Quantize model to save memory",
"parameters": {
"model": "str (source model)",
"bits": "int (4 or 8)",
"output": "str (output path)"
},
"returns": "Quantized model path"
},
# Kubernetes/Docker
"list_pods": {
"description": "List Kubernetes pods in namespace",
"parameters": {"namespace": "str"},
"returns": "List of pods with status"
},
"get_pod_logs": {
"description": "Get logs from specific pod",
"parameters": {
"pod": "str (pod name)",
"lines": "int"
},
"returns": "Recent log lines"
},
"restart_pod": {
"description": "Restart a Kubernetes pod",
"parameters": {"pod": "str"},
"returns": "Restart confirmation"
},
# File System
"read_file": {
"description": "Read contents of a file",
"parameters": {"path": "str"},
"returns": "File contents"
},
"write_file": {
"description": "Write content to a file",
"parameters": {
"path": "str",
"content": "str"
},
"returns": "Write confirmation"
},
"search_files": {
"description": "Search for files by pattern",
"parameters": {
"path": "str",
"pattern": "str"
},
"returns": "Matching file paths"
},
# Code Execution
"run_python": {
"description": "Execute Python code in sandbox",
"parameters": {"code": "str"},
"returns": "Execution result or error"
},
"run_bash": {
"description": "Execute bash command",
"parameters": {"command": "str"},
"returns": "Command output"
},
# Web/Network
"web_search": {
"description": "Search the web",
"parameters": {
"query": "str",
"num_results": "int"
},
"returns": "Search results"
},
"fetch_url": {
"description": "Fetch content from URL",
"parameters": {"url": "str"},
"returns": "Page content"
},
# Database
"query_vector_db": {
"description": "Query Qdrant vector database",
"parameters": {
"collection": "str",
"query": "str",
"limit": "int"
},
"returns": "Search results with scores"
},
"query_knowledge_graph": {
"description": "Query Neo4j knowledge graph",
"parameters": {"cypher": "str"},
"returns": "Graph query results"
}
}
# Agent Role Tools
tools_coder = {
"read_file": tools_local["read_file"],
"write_file": tools_local["write_file"],
"run_python": tools_local["run_python"],
"run_bash": tools_local["run_bash"],
"search_files": tools_local["search_files"]
}
tools_ops = {
"nvidia_smi": tools_local["nvidia_smi"],
"list_pods": tools_local["list_pods"],
"get_pod_logs": tools_local["get_pod_logs"],
"restart_pod": tools_local["restart_pod"]
}
tools_researcher = {
"web_search": tools_local["web_search"],
"fetch_url": tools_local["fetch_url"],
"query_vector_db": tools_local["query_vector_db"]
}
Module Structure¶
[7100] Agent Architecture & Cognitive Systems¶
| Document | Description | Time | Difficulty |
|---|---|---|---|
| 7101: Agent Architecture | Thought, Action, Observation patterns | 4h | Advanced |
| 7102: State Machines | Breaking down complex tasks | 4h | Advanced |
| 7103: ReAct Implementation | Complete ReAct implementation | 4h | Advanced |
| 7104: Agent Evaluation and Observability | Score trajectories, judge mechanics, trace budgets, regression gates | 4h | Advanced |
| 7105: Reflection and Self-Correction | Refine loops, self-consistency votes, the intrinsic-vs-informed correction boundary | 4h | Advanced |
| 7106: Function-Calling Training and Constrained Decoding | The grammar-to-mask mechanism, prompting-vs-masking, the pair-trained call | 4h | Advanced |
What You'll Learn:
- ReAct (Reasoning + Acting) pattern
- Task decomposition and planning
- State machines for agent control
- Cognitive architectures
- Agent evaluation: scorers, LLM judges, and confidence intervals
- Reflection and self-correction: refine loops, plurality votes, and the correction boundary
- Function-calling training and constrained decoding: the grammar-to-mask mechanism and the pair-trained call
Hands-On Practice:
- Implement ReAct loop from scratch
- Build task planning system
- Create state machine for complex workflows
- Debug agent reasoning chains
- Score recorded trajectories and gate CI on regressions
- Price one batch under five self-improvement policies and pick by the bill
- Flip a call grammar's validity with a mask and price the four serving stacks
[7200] Tool Use & Function Calling¶
| Document | Description | Time | Difficulty |
|---|---|---|---|
| 7201: Tool Calling | OpenAI-style function calling | 3h | Intermediate |
| 7202: Code Interpreter | Sandboxed code execution | 5h | Advanced |
| 7203: MCP Hands-On | Tool servers and clients over MCP | 4h | Intermediate |
| 7204: Timeouts, Retries, and Rate Limits | The client-side resilience triad as working code | 4h | Intermediate |
What You'll Learn:
- Function calling with LLMs
- Tool schema definition
- Sandboxed code execution
- Error handling and recovery
- MCP tool servers and clients over one standard surface
- Timeout budgets, idempotent retries, jittered backoff, and the caller-side rate-limit bucket
Hands-On Practice:
- Implement tool calling system
- Build code interpreter
- Create custom tools
- Handle tool failures gracefully
- Build an MCP server and walk the client loop
[7300] Multi-Agent Orchestration¶
| Document | Description | Time | Difficulty |
|---|---|---|---|
| 7301: Orchestration | Multi-agent collaboration | 4h | Advanced |
| 7302: Communication Protocols | Agent-to-agent messaging | 4h | Advanced |
| 7303: Framework Comparison | AutoGen vs LangGraph vs others | 3h | Advanced |
| 7304: Event Buses, Deadlocks, and Human Gates | Orchestration runtime mechanics | 3h | Advanced |
What You'll Learn:
- Multi-agent patterns (hierarchical, sequential, parallel)
- Agent communication protocols
- Orchestration strategies
- Framework selection and comparison
- Delivery semantics, deadlock detection, and mid-run human gates
Hands-On Practice:
- Build multi-agent system
- Implement agent communication
- Create specialized agent roles
- Orchestrate complex workflows
- Break a three-agent ring with a wait-for graph and fix it with a global lock order
[7400] Agent Memory¶
| Document | Description | Time | Difficulty |
|---|---|---|---|
| 7401: Long-term Memory | Persistent memory systems | 4h | Advanced |
| 7402: Memory Implementation | Memory architecture guide | 3h | Advanced |
| 7403: Vector Memory | Embedding-based memory | 4h | Advanced |
| 7404: Reflective Memory and Context Paging | Reflection, paging, retrieval blend | 3h | Advanced |
| 7405: Key-Value Memory and Checkpoint Stores | Exact-key stores, TTL leases, eviction, checkpoints | 3h | Advanced |
What You'll Learn:
- Memory architectures (short-term, long-term, episodic)
- Vector-based memory systems
- Memory retrieval and ranking
- Persistent storage strategies
- Rank memories with the recency-importance-relevance blend, distill episodes before pruning, and page a 48-token context without amnesia
- Read the exact-key store underneath: the address space, TTL lease mechanics, eviction budgets, and the checkpoint contract
Hands-On Practice:
- Implement memory system
- Build vector memory store
- Create memory retrieval mechanisms
- Optimize memory performance
- Walk a keyspace through passive versus active expiration and price four memory configs on one ledger
[7500] AI Agent Security¶
| Document | Description | Time | Difficulty |
|---|---|---|---|
| 7501: Prompt Injection Defense | Injection detection and prevention | 3h | Advanced |
| 7502: PII Redaction | Privacy filtering and compliance | 3h | Advanced |
| 7503: Adversarial Attacks | Attack types and defense layers | 3h | Advanced |
| 7504: Agent Abuse Prevention and Identity Threats | Rate limiting, agent identity, egress monitoring | 3h | Advanced |
| 7505: Temperature Zero and Deterministic Security Testing | Deterministic decoding for reproducible security tests | 3h | Advanced |
What You'll Learn:
- Prompt injection techniques and defense layers
- PII detection, redaction, and compliance (GDPR, HIPAA)
- Adversarial attack types (jailbreak, DAN, roleplay)
- Input validation and output filtering
- Red teaming agent systems
- Abuse mechanics: rate limiting, agent identity, egress beacons, payload entropy
- Deterministic decoding: temperature zero as argmax, seed limits, pass@k
Hands-On Practice:
- Build an injection detector
- Implement a PII redaction system
- Test agents with adversarial prompts
- Design a multi-layer defense pipeline
- Break a rate limiter at the seam and flag an egress beacon
- Pin temperature to zero and price a red-team CI gate four ways
Learning Path¶
Recommended Flow¶
graph TD
A[Start: Phase 6 Complete] --> B{Your Goal?}
B -->|Build autonomous agent| C[7100: Architecture]
B -->|Add tool capabilities| D[7200: Tool Use]
B -->|Coordinate multiple agents| E[7300: Orchestration]
B -->|Add persistent memory| F[7400: Memory]
B -->|Secure your agents| O[7500: Security]
C --> G[7101: ReAct Loop]
G --> H[7103: Implementation]
D --> I[7201: Tool Calling]
I --> J[7202: Code Interpreter]
E --> K[7301: Orchestration]
K --> L[7303: Framework Comparison]
F --> M[7401: Long-term Memory]
M --> N[7402: Implementation]
O --> P[7501: Prompt Injection]
P --> Q[7503: Adversarial]
style C fill:#e1f5fe
style D fill:#fff3e0
style E fill:#f3e5f5
style F fill:#fce4ec
style O fill:#e8f5e9
Time Estimates¶
| Module | Reading | Practice | Total |
|---|---|---|---|
| 7100: Architecture | 24h | 7h | 31h |
| 7200: Tool Use | 16h | 3h | 19h |
| 7300: Orchestration | 14h | 6h | 20h |
| 7400: Memory | 17h | 6h | 23h |
| 7500: Security | 15h | 13h | 28h |
| Total | 86h | 35h | 121h |
Cheat Sheets¶
Single-page references for the surfaces this phase teaches:
- CHEAT-SHEET-012: LLM Guardrails & Agent Security
- CHEAT-SHEET-013: Structured Output & Function Calling
- CHEAT-SHEET-014: Decoding & Sampling Strategies
- CHEAT-SHEET-023: ReAct Agent Loop
- CHEAT-SHEET-024: Multi-Agent Orchestration
- CHEAT-SHEET-025: Agent Memory Systems
- CHEAT-SHEET-026: Agent Security
- CHEAT-SHEET-027: Tool Calling and MCP
- CHEAT-SHEET-028: Agent Evaluation and Observability
- CHEAT-SHEET-029: Function Calling Training and Constrained Decoding
- CHEAT-SHEET-030: Multi-Agent Communication Protocols
- CHEAT-SHEET-031: Multi-Agent Framework Comparison
- CHEAT-SHEET-032: Code Interpreter Sandbox
- CHEAT-SHEET-036: Vector Memory - the Embedding-Backed Store
- CHEAT-SHEET-037: Reflection & Self-Correction - the Loop That Grades Its Own Work
- CHEAT-SHEET-057: Browser and Computer-Use Agents - the Grounding Verdict, the Step Budget, and the Sandbox Gate Chain
- CHEAT-SHEET-058: Agent Skills - the Loading Ladder, the Trigger Verdict, and the Packaging Gate
- CHEAT-SHEET-060: Agentic RAG - the Escalation Gate, the Loop Budget, and the Trajectory Audit
- CHEAT-SHEET-061: Compound AI Systems - the Composition Ledger, the Verifier Gate, and the Ablation Audit
- Volume 7: Production Mastery - Quick Reference - the full command companion for this phase
Key Takeaways¶
You Will Learn¶
After completing this phase, you will be able to:
-
Build Autonomous Agents
- Implement ReAct reasoning loops
- Create planning and task decomposition
- Build state machines for control flow
- Debug agent reasoning chains
-
Implement Tool Calling
- Define tool schemas
- Execute functions safely
- Build code interpreters
- Handle errors gracefully
-
Orchestrate Multi-Agent Systems
- Design agent architectures
- Implement communication protocols
- Coordinate specialized agents
- Choose the right framework
-
Create Memory Systems
- Build persistent memory
- Implement vector-based retrieval
- Store episodic memories
- Optimize memory performance
-
Deploy Production Agents
- Secure tool execution
- Monitor agent behavior
- Scale multi-agent systems
- Debug complex workflows
Common Pitfalls¶
Pitfall 1: Infinite Loops¶
Pitfall: Agent gets stuck in reasoning loop
# Wrong: No max iterations
import time
while not task_complete:
thought = agent.think()
action = agent.act()
observation = execute(action)
# May never converge!
# Right: Add max iterations and timeout
max_iterations = 10
timeout = 60 # seconds
for i in range(max_iterations):
thought = agent.think()
action = agent.act()
observation = execute(action)
if task_complete or time.time() > timeout:
break
Pitfall 2: Unsafe Tool Execution¶
Pitfall: Executing arbitrary code without sandboxing
# Wrong: Direct execution
result = eval(user_code) # DANGEROUS!
# Can delete files, steal data, etc.
# Right: Sandboxed execution
from restrictedpython import compile_restricted
import subprocess
# Use Docker or restricted Python
result = run_in_sandbox(user_code, timeout=30)
Pitfall 3: Poor Tool Selection¶
Pitfall: Agent calls wrong tools repeatedly
# Wrong: No tool descriptions
tools = ["search", "read", "write"]
# Agent guesses what each does
# Right: Detailed tool schemas
tools = [
{
"name": "search_web",
"description": "Search the internet for current information",
"parameters": {
"query": {"type": "string", "description": "Search query"}
}
}
]
Pitfall 4: Missing Error Handling¶
Pitfall: Agent fails on tool errors
# Wrong: No error handling
result = tool.call(**args)
# Agent crashes on failure
# Right: Graceful error handling
try:
result = tool.call(**args)
observation = {"status": "success", "result": result}
except Exception as e:
observation = {
"status": "error",
"message": str(e),
"recovery": "Try alternative approach"
}
Pitfall 5: No Memory Retrieval¶
Pitfall: Agent forgets previous interactions
# Wrong: Stateless agent
response = agent.generate(query)
# Each query is independent
# Right: Memory-enabled agent
memory = agent.memory.retrieve(query, k=5)
response = agent.generate(
query,
context=memory,
history=conversation_history
)
Pro Tips¶
ReAct Loop Design¶
Tip: Structure thoughts for better reasoning
# Thought template for consistent reasoning
thought_template = """
Current Task: {task}
Previous Actions: {history}
Available Tools: {tools}
What I know:
{context}
What I need to find out:
{missing}
Next step:
{next_step}
Tool to use: {tool}
Tool parameters: {params}
"""
thought = thought_template.format(
task=task,
history=action_history,
tools=available_tools,
context=current_context,
missing=missing_info,
next_step=next_action,
tool=selected_tool,
params=tool_params
)
Agent Role Definition¶
Tip: Clear role definitions improve multi-agent systems
agent_roles = {
"researcher": {
"goal": "Gather information from external sources",
"tools": ["web_search", "fetch_url", "read_file"],
"expertise": "Research and information synthesis",
"constraints": "Cannot execute code or modify files"
},
"coder": {
"goal": "Write and modify code",
"tools": ["read_file", "write_file", "run_python"],
"expertise": "Software development",
"constraints": "Only access approved directories"
},
"reviewer": {
"goal": "Review and validate work",
"tools": ["read_file", "static_analysis"],
"expertise": "Code review and quality assurance",
"constraints": "Cannot modify files, only report issues"
}
}
Tool Schema Best Practices¶
Tip: Detailed schemas improve tool selection
# Good tool schema
tool_schema = {
"name": "query_database",
"description": """
Query the PostgreSQL database for customer information.
Use this when you need to look up customer details, orders, or transactions.
Returns structured data with customer information.
""",
"parameters": {
"table": {
"type": "string",
"description": "Table to query (customers, orders, products)",
"enum": ["customers", "orders", "products"]
},
"filters": {
"type": "object",
"description": "Key-value pairs for WHERE clause",
"examples": [
{"customer_id": 123},
{"status": "active"}
]
}
},
"returns": {
"type": "array",
"description": "List of matching records"
}
}
Memory Chunking Strategy¶
Tip: Organize memory for efficient retrieval
# Store memories with metadata and timestamps
memory_entry = {
"content": "User prefers Python over JavaScript",
"type": "preference",
"source": "conversation",
"timestamp": "2024-01-15T10:30:00Z",
"importance": 0.8, # 0-1 score
"entities": ["user", "python", "javascript"],
"embed": embedding_model.encode(content)
}
# Retrieve with time decay and relevance
retrieved = memory.search(
query="programming language",
weight_recent=0.3, # Prefer recent memories
weight_relevant=0.7, # But relevance matters more
k=5
)
Multi-Agent Communication¶
Tip: Structured message protocols
import time
import uuid
class AgentMessage:
def __init__(self, sender, receiver, type, content):
self.sender = sender # Agent ID
self.receiver = receiver # Agent ID
self.type = type # "request", "response", "notification"
self.content = content
self.timestamp = time.time()
self.id = str(uuid.uuid4())
def to_dict(self):
return {
"id": self.id,
"sender": self.sender,
"receiver": self.receiver,
"type": self.type,
"content": self.content,
"timestamp": self.timestamp
}
# Usage
message = AgentMessage(
sender="researcher",
receiver="writer",
type="request",
content={
"task": "write_summary",
"topic": "AI agents",
"sources": ["url1", "url2"]
}
)
Performance Benchmarks¶
Agent Performance Comparison¶
| Agent Type | Response Time | Token Usage | Success Rate | Best For |
|---|---|---|---|---|
| Simple ReAct | 5-15s | 500-2K | 70% | Quick tasks |
| ReAct + Tools | 10-30s | 1K-5K | 85% | Single-agent workflows |
| Multi-Agent (2-3) | 20-60s | 2K-10K | 90% | Complex tasks |
| Multi-Agent (5+) | 30-120s | 5K-20K | 95% | Complex, specialized tasks |
| Multi-Agent + Memory | 15-45s | 2K-8K | 92% | Long-running processes |
Framework Performance¶
| Framework | Setup Time | Runtime Overhead | Ease of Debugging |
|---|---|---|---|
| LangGraph | Medium | Low | Excellent |
| AutoGen | Low | Medium | Good |
| CrewAI | Low | Medium | Good |
| OpenAI Agents SDK | Very Low | Low | Medium |
| LangChain | High | Medium | Medium |
Cost Analysis (GPT-4)¶
| Agent Type | Cost per 100 Turns | Monthly Cost (1K turns) |
|---|---|---|
| Simple ReAct | ~$2 | ~$20 |
| ReAct + Tools | ~$5 | ~$50 |
| Multi-Agent (3 agents) | ~$12 | ~$120 |
| Multi-Agent + Memory | ~$8 | ~$80 |
Related Experiments¶
Hands-on Practice¶
-
- Implement ReAct loop from scratch
- Build task planning system
- Debug agent reasoning
-
- Implement function calling
- Build code interpreter
- Create custom tools
-
- Build multi-agent system
- Implement communication protocols
- Orchestrate complex workflow
-
- Implement memory system
- Build vector memory store
- Optimize retrieval
-
- Run injection attacks against an agent
- Measure multi-layer defense effectiveness
- Detect injections with perplexity
Prerequisites¶
Before starting this phase, ensure you understand:
- LLM Fundamentals (from 2400: Pretraining)
- RAG Systems (from 6100: Vector Embeddings)
- API Design (REST, webhooks)
See PREREQUISITES for details.
Assessment¶
Validate your knowledge with:
- Phase 7 Checkpoint - Module-by-module phase-exit review
- Phase 7 Quiz - Test your understanding (30 questions, 80% to pass)
- Phase 7 Practice - Hands-on exercises
Related Topics¶
- 5100: PEFT - Fine-tune models for agents
- 6100: RAG - Add memory to agents
- 7500: Security - Secure deployed agents
- 1500: Monitoring - Request-level observability underneath these traces
- SOL-001: Enterprise KB - RAG + Agent solution
- Industry Guides: Domain-specific agents
Next Steps¶
After completing this phase:
-
Build Your Agents
- Create autonomous code assistant
- Build DevOps automation agent
- Deploy multi-agent team
-
Continue Learning
- Solutions: End-to-end implementations
- Industry Guides: Domain-specific agents
- Projects: Real-world applications
Quick Reference¶
ReAct Pattern¶
Thought → Action → Observation → Thought → Action → Finish
Example:
1. Thought: User wants to know the weather in Tokyo
2. Action: call_weather(location="Tokyo")
3. Observation: {"temp": 22, "condition": "clear"}
4. Thought: Have the information, can respond
5. Finish: "It's currently 22°C and clear in Tokyo."
Multi-Agent Patterns¶
| Pattern | Description | Use Case |
|---|---|---|
| Hierarchical | Manager coordinates workers | Complex task breakdown |
| Sequential | Pipeline processing | Multi-step workflows |
| Parallel | Independent agents | Speed optimization |
| Consensus | Voting mechanism | Decision making |
Additional Diagrams:
ReAct Agent Diagrams:
- REACT-LOOP.md - Complete ReAct loop visualization
- ReAct Loop Architecture (State Diagram)
- Detailed ReAct Sequence
- Tool Calling Flow
- Multi-Step Reasoning Example
- ReAct vs Standard LLM
- Error Handling in ReAct
Project Architecture Diagrams:
- PROJECT-001-ARCHITECTURE.md - AI Assistant system architecture
Module Duration: 121 hours (86 reading + 35 practice)
Difficulty: ⭐⭐⭐ Advanced
Ready to build autonomous agents? Start with 7101: Agent Architecture or 7201: Tool Calling