Tool Calling
Invoke external functions from PredictionGuard’s /chat/completions
endpoint
Please use the /models endpoint to see which models support tool calling.
Overview
Tool calling extends your model’s capabilities beyond static text generation. With tool calling, models can:
- Request real-time information (weather, stock prices)
- Execute business workflows (create tickets, send emails)
- Chain multiple operations together
- Integrate with your existing APIs and services
Core Concepts
Tools
Functions you expose to the model, defined using JSON Schema. Each tool includes:
- Name: Unique identifier for the function
- Description: Clear explanation of what the tool does
- Parameters: Expected inputs with types and constraints
Tool Calls
When the model determines a tool would help answer a query, it generates a tool call with:
- The tool name to invoke
- Arguments formatted as JSON
- A unique call ID for tracking
Tool Choice Strategy
Controls how the model decides whether to use tools:
Quick Start
1. Define Your Tool
Create a JSON schema describing your function:
Write clear, specific descriptions for both the tool and its parameters. This helps the model understand when and how to use your tools correctly. Set strict: true
to enforce exact schema adherence.
2. Make a Request with Tools
Include your tool definitions in the API request:
3. Handle the Tool Call
When the model requests a tool, you’ll receive:
4. Execute and Return Results
Parse the arguments, execute your function, and send back the result:
5. Get the Final Response
The model incorporates the tool result and provides a natural language response:
Best Practices
Tool Design
Keep tools focused: Each tool should have a single, clear purpose
Use explicit parameters: Avoid ambiguous or overly flexible inputs
Provide clear descriptions: Help the model understand when to use each tool
Error Handling
Build robust error handling for production use:
Security Considerations
Validate all inputs: Never trust model-generated arguments
Check for prompt injections: Use PredictionGuard’s injection detection API
PredictionGuard provides a dedicated Injection API to detect prompt injection attacks. Consider checking user inputs before processing them with tool-enabled models to prevent malicious attempts to manipulate tool behavior.
Implement access controls: Verify permissions before execution
Never expose sensitive data: Keep API keys and secrets server-side
Troubleshooting
Model Skips Required Tools
If tool_choice: "auto"
isn’t working:
- Add explicit instructions in the user message
- Use named tool forcing
- Ensure tool descriptions are clear
Invalid Arguments
Common causes and solutions:
- Missing parameters: Add them to
required
array - Wrong types: Use
enum
for constrained values - Complex objects: Flatten nested structures
Performance Issues
Optimize tool calling performance:
- Cache frequently requested data
- Set reasonable timeouts
Examples
Weather Assistant
Customer Support Bot
Next Steps
- Explore our SDK examples for language-specific implementations
Support
Need help with tool calling?
- 📚 API Reference: docs.predictionguard.com
- 📧 Join Our Discord: Discord