
AI Computing
Lesson Content
Building Your First AI Agent
The Trial and Error Approach
Building AI agents requires experimentation. Learn from failures and iterate!
Agent Design Process
Step 1: Define the Goal
What should your agent accomplish?
Example Goal: "Research assistant that finds and summarizes papers"
Step 2: Identify Required Tools
- Web search
- PDF reader
- Summarization
- Note-taking
Step 3: Design the Agent Loop
codewhile not goal_achieved: thought = agent.think(current_state) action = agent.plan_action(thought) observation = agent.execute(action) current_state = agent.update_state(observation)
Implementation Example
Basic ReAct Agent
codeclass ResearchAgent: def __init__(self, llm, tools): self.llm = llm self.tools = tools self.memory = [] def run(self, task): max_iterations = 10 for i in range(max_iterations): # Think prompt = self.create_prompt(task) response = self.llm.generate(prompt) # Parse thought and action thought = self.extract_thought(response) action = self.extract_action(response) self.memory.append({ 'thought': thought, 'action': action }) # Act if action['type'] == 'FINISH': return action['result'] observation = self.execute_tool( action['tool'], action['input'] ) self.memory.append({ 'observation': observation }) return "Max iterations reached"
Common Failure Modes
1. Infinite Loops
Problem: Agent repeats same action
Solution:
- Add max iterations
- Track action history
- Detect repetition patterns
2. Tool Misuse
Problem: Wrong tool for task
Solution:
- Better tool descriptions
- Few-shot examples
- Tool validation logic
3. Context Overflow
Problem: Memory grows too large
Solution:
- Summarize old memories
- Keep only relevant context
- Use vector databases
4. Hallucinated Actions
Problem: Invents non-existent tools
Solution:
- Strict action parsing
- Validation before execution
- Retry with feedback
Trial and Error Examples
Iteration 1: Simple Prompt
code"Search for papers about transformers"
Result: Too vague, returns web results
Iteration 2: More Specific
code"Search for academic papers about transformer neural networks published after 2020"
Result: Better, but still mixed results
Iteration 3: Structured Instructions
code"Use the scholar_search tool to find papers. Search query: 'transformer neural networks' Filters: year > 2020, type: peer-reviewed"
Result: Exactly what we need!
Best Practices
- Start Simple: Single tool, clear goal
- Log Everything: Debug with full traces
- Test Edge Cases: Handle failures gracefully
- Iterate Prompts: Refine instructions
- Add Guardrails: Validate inputs/outputs
- Monitor Costs: Track API usage
Advanced Patterns
Chain of Thought Prompting
codeLet's think step by step: 1. First, I need to... 2. Then, I should... 3. Finally, I can...
Self-Reflection
codeDid my last action work? If not, what should I try differently?
Multi-Agent Collaboration
codeResearcher Agent -> Finds papers Analyzer Agent -> Reads and extracts key points Writer Agent -> Creates summary
Debugging Tips
- Print full prompt and response
- Visualize agent's thought process
- Test tools individually first
- Use smaller, faster models for iteration
- Keep examples of good/bad runs
Next Steps
- Add memory systems (vector DBs)
- Implement error recovery
- Create specialized tools
- Build multi-agent systems
- Deploy with monitoring
Remember: Every failure teaches you something. Embrace the trial and error!