Skip to main content

What Makes Tools Awesome?

Think of tools as your agent’s superpowers! While agents are great at conversation, tools let them actually do things - like looking up orders, sending emails, or integrating with your favorite APIs. It’s like giving your AI a Swiss Army knife!

Understanding Tools

Here’s what makes Vizra tools special:

Simple PHP Classes

Just implement the ToolInterface and you’re ready to roll!

Self-Describing

JSON Schema definitions help LLMs understand exactly how to use your tools

Auto-Integration

Prism automatically connects your tools to the LLM’s function calling

Context-Aware

Access the full AgentContext for stateful operations

Creating Your First Tool

Quick Start with Artisan - The easiest way to create a tool? Let Artisan do the heavy lifting!
Terminal
This creates a fully-functional tool template in app/Tools/ ready for your custom logic!

Tool Structure

Every tool follows a simple pattern - implement the ToolInterface and define two key methods:
app/Tools/OrderLookupTool.php

Tool Interface Methods

The definition() Method

This is where you tell the LLM exactly what your tool does and what parameters it needs. Think of it as your tool’s instruction manual!
Tool Definition Example

Clear Names

Use descriptive names that tell the LLM exactly what your tool does

Good Descriptions

Help the LLM understand when and how to use your tool

Type Safety

Define parameter types to ensure correct usage

The execute() Method

This is where the magic happens! Your tool receives arguments from the LLM and the current context, then returns results as JSON.
Tool Execution Example
Remember: Always return a JSON-encoded string! The LLM expects structured data it can understand.

Working with AgentContext

The AgentContext is your tool’s memory bank! It lets you access session info, store state between calls, and maintain context across conversations.
Using AgentContext

Reading State

Use getState() to retrieve previously stored values

Writing State

Use setState() to persist data for future tool calls

Connecting Tools to Agents

Ready to give your agent superpowers? Just add your tools to the agent’s $tools array and watch the magic happen!
app/Agents/CustomerSupportAgent.php
Tools are automatically instantiated and made available to the LLM. Just list them and Vizra handles the rest!

Advanced Tool Features

Let’s explore some powerful patterns for building sophisticated tools!

Database Queries

Connect your tools directly to your database for powerful data operations:
Database Search Tool

API Integration

Connect to external APIs and bring real-world data into your conversations:
External API Tool

File Operations

Handle file uploads, downloads, and processing with ease:
File Handler Tool

Error Handling & Validation

Build bulletproof tools with proper validation and error handling! Your agents will thank you.

Input Validation

Always validate your inputs - it’s the first line of defense against errors:
Input Validation Example

User Context Access

Check for user authentication and access user-specific data safely:
User Context Validation

Tools with Memory Access

Every Tool Gets Memory Access! - All tools now receive the agent’s memory as a third parameter. Build personalized experiences by reading and writing to memory!
The execute method now includes AgentMemory:
app/Tools/UserProfileTool.php

Memory Methods Available

  • addFact() - Store immutable facts
  • addLearning() - Track insights
  • addPreference() - Store preferences
  • updateSummary() - Update user profile

Use Cases

  • Update user preferences from form submissions
  • Store discovered facts during conversations
  • Build comprehensive user profiles over time
  • Sync memory across different tools
Pro Tip: Simple Memory Usage - Every tool automatically receives the agent’s memory! Use it to store learnings, facts, and preferences. The memory persists across sessions, enabling truly personalized experiences.

Testing Your Tools

Great tools deserve great tests! Here’s how to ensure your tools work perfectly every time:
tests/Tools/OrderLookupToolTest.php
Testing tip: Test both success paths and error conditions. Your future self will appreciate it!

Complete Example: Refund Processor

Let’s put it all together with a real-world example that shows validation, error handling, and business logic!
app/Tools/RefundProcessorTool.php

Tool Best Practices

Do's

  • Single Responsibility - Each tool should do one thing and do it well
  • Input Validation - Always validate parameters before processing
  • Clear Error Messages - Help the LLM understand what went wrong
  • Descriptive Naming - Use names that clearly describe the tool’s purpose

More Do's

  • Error Handling - Gracefully handle exceptions and edge cases
  • Rate Limiting - Protect expensive operations from abuse
  • Thorough Testing - Test success paths and error conditions
  • JSON Responses - Always return properly formatted JSON strings

Sessions & Memory

Learn about context management and persistent state

Tool API Reference

Detailed tool class documentation and methods