Prompt
what's the best way to build an AI agent that can use tools and APIs?
Latest observation
The best way is to build it as a tool-using workflow with a strong control loop, not just a model that “knows” tools exist.
Recommended architecture
1) Use the model for reasoning, not execution
Have the LLM do:
- interpret the user’s goal
- decide which tool to call
- generate structured arguments for the tool
- summarize results and decide next steps
Have your app do:
- authentication
- API requests
- error handling
- retries/timeouts
- state management
- logging and auditing
2) Define tools with strict schemas
Each tool should have:
- a clear name
- a description
- JSON input schema
- defined output format
Example:
{
"name": "search_docs",
"description": "Search internal documentation for relevant info",
"parameters": {
"type": "object",
"properties": {
"query": { "type": "string" }
},
"required": ["query"]
}
}
This reduces hallucinations and makes tool use reliable.
3) Implement an agent loop
A typical loop:
- User asks something
- LLM decides whether to answer directly or call a tool
- If tool call is needed, your app executes it
- Return tool result to LLM
- LLM either:
- answers the user
- calls another tool
- asks for clarification
Keep this loop bounded, e.g. max 5–10 steps.
4) Keep state outside the model
Store:
- conversation history
- tool outputs
- user preferences
- intermediate tasks
Don’t rely on the model to “remember” everything. Use your own memory/state layer.
5) Add guardrails
Important safeguards:
- allowlist tools only
- validate all tool inputs
- sanitize outputs
- prevent arbitrary code execution unless sandboxed
- require human confirmation for risky actions
- set rate limits and permission scopes
6) Make tools atomic and composable
Prefer small tools:
get_customerupdate_ticketsend_emailsearch_orders
Instead of one giant “do everything” tool. Smaller tools are easier to test and combine.
7) Use retrieval when knowledge matters
If the agent needs company docs, policies, or product info:
- index documents in a vector store or search engine
- retrieve relevant context before the LLM reasons
- cite sources when possible
8) Log everything
Track:
- user request
- tool chosen
- tool inputs/outputs
- model decisions
- failures and retries
This is essential for debugging and safety.
Practical stack
A solid setup might be:
- LLM: GPT-style model with function/tool calling
- Orchestration: your app server or a framework
- Tools: REST/GraphQL/internal functions
- Memory: database + vector store
- Monitoring: logs + traces + evals
Frameworks can help, but you don’t need one. A simple custom loop is often best for production reliability.
Best practice pattern
For most production agents:
- Plan
- Act
- Observe
- Repeat
- Finalize
This is the core pattern behind tool-using agents.
Common mistakes
Avoid:
- letting the model directly “call” external APIs without validation
- giving it too many tools at once
- making tools return huge unstructured blobs
- no retry/error handling
- no human approval for destructive actions
- no evals before launch
If you want the simplest good approach
Start with:
- a single LLM
- 3–10 well-defined tools
- strict JSON schemas
- a bounded agent loop
- logging and evals
- human approval for sensitive actions
If you want, I can also give you:
- a reference architecture diagram
- a minimal Python implementation
- or a production checklist for tool-using agents.
Brands