Custom Tools
Extending What Your Agent Can Do
Tools Are the Hands of Your Agent
An agent without tools can only think and talk. Tools give it the ability to act — reading files, processing data, calling APIs, and writing output. The quality and safety of your tools determines the quality and safety of your agent.
Your project comes with three starter tools: a file organizer, a PDF summarizer, and an expense tracker. In this module, you'll understand how they work and build three more.
Anatomy of a Safe Tool
Every tool in OpenClaw follows a four-step pattern:
1. VALIDATE → Check inputs are well-formed and within expected ranges
2. PERMIT → Verify the action is allowed by safety rules
3. ACT → Execute the actual operation
4. LOG → Record what happened in the audit trailThis pattern ensures that no tool can bypass safety checks, and every action is accountable.
Input Validation
Input validation is your first defense against unexpected behavior:
| Check | Why It Matters |
|---|---|
| Type checking | Prevent "undefined" from becoming a file path |
| Range validation | Prevent negative amounts, dates in the future, impossibly large files |
| Path sanitization | Prevent path traversal attacks (e.g., `../../etc/passwd`) |
| String length limits | Prevent memory issues from absurdly long inputs |
| Required fields | Catch missing data before it causes cryptic errors downstream |
Never trust inputs from the LLM. The model might hallucinate file paths, invent dates, or pass malformed data. Validate everything.
Permission Checks
Before a tool acts, it must verify the action is allowed:
If any check fails, the tool should return a clear error message (not silently skip) and log the denial.
The Three Starter Tools
Your project includes three tools that demonstrate the pattern:
File Organizer (src/tools/file-organizer.ts)
data/downloads.json for file entriesPDF Summarizer (src/tools/pdf-summarizer.ts)
data/documents.json for document metadataExpense Tracker (src/tools/expense-tracker.ts)
data/expenses.json for receipt dataBuilding New Tools
When building a new tool, start with this checklist:
Edge Cases That Break Tools
The LLM will send your tools unexpected inputs. Plan for:
A well-designed tool handles all of these gracefully, with clear error messages and audit log entries.
Key Takeaways
This is chapter 4 of Open Source AI Agents (OpenClaw).
Get the full hands-on course — free during early access. Build the complete system. Your projects become your portfolio.
View course details