Skip to main content
Sonamu allows you to create custom AI agents by extending BaseAgentClass. By defining tools using the @tools decorator, the LLM can select and execute appropriate tools based on the context.

BaseAgentClass

This is the base class for AI agents.

Basic Structure

@tools Decorator

A decorator for defining tools.

Basic Usage

Options

Tool Description (required)
Important: The LLM reads this description to decide whether to use the tool.

Practical Examples

1. Hotel Booking Agent

2. Data Analysis Agent

A tool that runs LLM-generated SQL can be steered by prompt injection into running whatever SQL an attacker wants. A regex check such as /^SELECT/i does not stop multi-statement input like SELECT 1; DROP TABLE users, so the example below uses three layers.
  1. Least-privilege DB role: connect as a dedicated role that has SELECT only on allowed views. This is the real boundary that blocks writes and reads of sensitive tables.
  2. Single-statement parser: use the PostgreSQL parser (libpg-query) to check that the SQL is exactly one SELECT statement.
  3. Read-only transaction: run the query in a READ ONLY transaction with a statement_timeout.
First, create the views exposed to the AI and the dedicated role with a DB admin account.
Install the parser with pnpm add libpg-query, and put the connection settings in AI_ANALYST_DB_* environment variables.
Do not use BaseModel.getPuri("r") for this. Without a read-only replica, "r" connects with the same account as the main DB and has that account’s privileges, which usually include writing and reading every table.

3. Email Automation Agent

Using Agents

use() Method

This method executes the agent.

Usage in API

Store (State Management)

Manages the agent’s state.

Definition

Usage

Precautions

Important considerations when writing agents:
  1. Tool Description Clarity: Write detailed descriptions that the LLM can understand
  2. Detailed Input Schema: Use describe()
  3. Require Approval for Dangerous Operations: needsApproval
  4. Error Handling: Clear error messages
  5. Check for Undefined When Accessing Store
  6. Avoid Tool Name Conflicts: Use clear names

Next Steps

Agent Configuration

Configure agent basics

Using the AI SDK

Leverage the Vercel AI SDK