Skip to main content

Overview

Tool functions provide agents with capabilities to query structured databases and search unstructured knowledge bases. All tools are wrapped with LangSmith’s traceable decorator for observability.

Database Tools

queryDatabase()

Executes SQL queries against a SQLite database and returns results as JSON.
string
required
SQL query to execute. The agent should first discover the schema using PRAGMA table_info() or SELECT name FROM sqlite_master WHERE type='table'
string
required
File path to the SQLite database file
string
JSON-stringified array of query results, or error message if query fails

Usage Example

Schema Discovery Pattern

The tool description instructs the agent to always discover the schema first:

Knowledge Base Tools

searchKnowledgeBase()

Searches the knowledge base using semantic similarity with embeddings.
string
required
Natural language search query or question
number
default:2
Number of top results to return
string
Formatted string containing the most relevant documents with relevance scores, or error if knowledge base not loaded

Usage Example

Chunking Strategy (v3)

Agent version 3 implements text chunking for better retrieval:
Chunked documents are indexed with identifiers:

Embeddings Cache Management

embeddingsAreStale()

Checks if cached embeddings need regeneration by comparing modification times.
string
required
Path to knowledge base documents directory
string
required
Path to embeddings cache file
boolean
True if embeddings need regeneration, false if cache is valid

generateAndCacheEmbeddings()

Generates embeddings for all knowledge base documents and caches them.
string
required
Path to knowledge base documents directory
string
required
Path where embeddings cache should be saved

Tool Integration in Agent

Tools are integrated into the agent using OpenAI’s function calling:

Best Practices

Database Queries

  1. Always discover schema first - Don’t assume table or column names
  2. Handle errors gracefully - Return error messages as strings
  3. Limit result sets - Use LIMIT to prevent overwhelming responses
  1. Load knowledge base on startup - Embeddings generation is expensive
  2. Use caching - Check for stale embeddings before regenerating
  3. Tune topK parameter - Balance context size vs. relevance
  4. Consider chunking - For large documents, chunk before embedding

Traceability

Both tools use LangSmith’s traceable wrapper for observability:
This enables:
  • Automatic trace logging
  • Performance monitoring
  • Debugging and error tracking
  • Cost analysis