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
OpenAI Tool Schema
Tool definition for OpenAI’s function calling:- Schema Discovery First: Always instruct agents to discover the database schema before querying data
- Error Handling: Wrap database operations in try/except blocks
- Connection Management: Close database connections after each query
- 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
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- 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
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:Related
- Agent Implementation - Agent patterns using these tools
- Evaluators - Testing tool usage