Skip to main content

Overview

Tool functions extend agent capabilities by providing access to external data sources and services. This reference documents the database query and knowledge base search tools used in the Building Reliable Agents course.

Database Tools

query_database()

Execute SQL queries against the inventory database.
str
required
SQL query to execute. Must be valid SQLite syntax.
str
required
Path to the SQLite database file (e.g., "./inventory/inventory.db")
str
Query results as a string representation of a list of tuples, or an error message if the query fails
Example:

OpenAI Tool Schema

Tool definition for OpenAI’s function calling:
Best Practices:
  1. Schema Discovery First: Always instruct agents to discover the database schema before querying data
  2. Error Handling: Wrap database operations in try/except blocks
  3. Connection Management: Close database connections after each query
  4. Query Validation: Validate SQL queries to prevent injection attacks in production

Knowledge Base Tools

search_knowledge_base()

Search company knowledge base documents using semantic similarity.
str
required
Natural language search query or question
int
default:"2"
Number of most relevant documents to return
str
Formatted string containing the top-k most relevant documents with relevance scores
Example:

OpenAI Tool Schema

load_knowledge_base()

Load knowledge base documents and generate embeddings (called at startup).
str
default:"./knowledge_base"
Path to knowledge base directory containing documents/ subfolder with markdown files
Example:
Features:
  • Automatic Caching: Embeddings are cached to avoid regenerating on every startup
  • Staleness Detection: Automatically detects when source documents change and regenerates embeddings
  • Whole Document Retrieval: Returns entire documents (not chunks) for better context

Text Chunking (Alternative Approach)

For large documents, you can chunk them before embedding:

chunk_text()

Split text into overlapping chunks.
str
required
Text to split into chunks
int
default:"200"
Size of each chunk in characters
int
default:"20"
Number of overlapping characters between chunks
list[str]
List of text chunks with overlap
Example:

Tool Integration Pattern

Combining multiple tools in an agent:

Best Practices

Tool Decoration

Always use @traceable with run_type="tool":

Schema Instructions

Provide clear instructions in tool descriptions:

Error Messages

Return helpful error messages:

Async Tools

Use async for I/O-bound operations: