Capstone: Build Your Agent
Build your own sophisticated AI agent from scratch as a capstone project
Your Progress
0 / 5 completedBuilding the Core Agent Logic
Your agent needs 4 core components working together: ReAct loop for reasoning, tool executor for actions, memory manager for context, and error handler for reliability.
Interactive: Core Component Explorer
Click each component to see implementation code and explanation:
🔄
ReAct Loop
class AgentCore:
def __init__(self, llm, tools):
self.llm = llm
self.tools = {t.name: t for t in tools}
self.memory = []
def run(self, task, max_steps=10):
for step in range(max_steps):
# THINK: Reason about next action
thought = self.llm.think(task, self.memory)
# ACT: Choose and execute tool
action = self.llm.choose_action(
thought, self.tools.keys()
)
if action.name == "FINISH":
return action.result
# OBSERVE: Get tool result
result = self.tools[action.name](
action.input
)
# Update memory
self.memory.append({
'thought': thought,
'action': action,
'result': result
})💡How It Works
Core ReAct loop: Think → Act → Observe. Repeat until task complete or max steps reached.
Integration Pattern
These 4 components work together in your main agent class:
class ProductionAgent:
def __init__(self, config):
self.llm = LLM(config.model)
self.tools = load_tools(config.tool_list)
# Initialize all 4 core components
self.react_loop = AgentCore(self.llm, self.tools)
self.executor = ToolExecutor(self.tools)
self.memory = MemoryManager(max_tokens=4000)
self.error_handler = ErrorHandler(max_retries=3)
def run(self, task):
try:
# Get relevant context from memory
context = self.memory.get_context(task)
# Run ReAct loop with context
result = self.react_loop.run(
task,
context=context,
executor=self.executor
)
# Save to memory
self.memory.add({
'task': task,
'result': result,
'steps': self.react_loop.memory
})
return result
except Exception as e:
# Handle errors gracefully
action = self.error_handler.handle(e, {
'task': task,
'retry_count': 0
})
if action == 'RETRY':
return self.run(task) # Recursive retry
elif action == 'FALLBACK':
return self.fallback_response(task)
else:
raise eBest Practices for Production
- •Logging: Log every tool call, error, and decision for debugging
- •Timeouts: Set max execution time per tool (30s) and total task (5min)
- •Cost Tracking: Count tokens per request, set budget limits
- •Validation: Validate tool inputs before execution, outputs before returning
🚀
Next Steps
Once your core components are working:
- 1.Test each component in isolation with unit tests
- 2.Test integration with realistic scenarios
- 3.Measure success rate, latency, cost per task
- 4.Iterate based on failures and bottlenecks