Best Practices
Safety & Guardrails
# Always implement safety checks
class SafeAgent:
def __init__(self, agent, max_iterations=10, max_cost=1.0):
self.agent = agent
self.max_iterations = max_iterations
self.max_cost = max_cost
self.iteration_count = 0
self.total_cost = 0
def invoke(self, input):
self.iteration_count += 1
if self.iteration_count > self.max_iterations:
raise Exception("Max iterations reached - possible infinite loop")
if self.total_cost > self.max_cost:
raise Exception("Cost limit exceeded")
return self.agent.invoke(input)
Key Principles
- Start simple — Begin with a single agent and few tools; add complexity gradually
- Limit scope — Define clear boundaries for what the agent can and cannot do
- Implement guardrails — Set max iterations, cost limits, and action restrictions
- Log everything — Record all agent decisions, tool calls, and outputs for debugging
- Human oversight — Keep humans in the loop for critical actions (payments, deletions, external communications)
- Test adversarially — Try to break your agent with edge cases and malicious inputs
- Use structured outputs — Force agents to respond in predictable formats (JSON, function calls)
- Handle failures gracefully — Agents will make mistakes; design for recovery