15. R1 with Tools
DeepSeek R1 excels at reasoning but lacks real-time information access. Tool integration extends reasoning models beyond static knowledge to live data, calculations, and external services.
The Tool-Augmented Architecture
A reasoning model with tools doesn't call them automatically—it reasons about when and how to use them. The architecture separates concerns: the model handles "what should I do" reasoning, while tools handle "execute this operation" execution.
User Query → Reasoning Model → Decide to use tool → Tool execution → Result
↓
Return to model
↓
Continue reasoning
↓
Final answer
This loop continues until the model decides it has sufficient information to answer.
Defining Tool Schemas
Tools expose functionality through JSON schemas that the model reads and interprets:
# Example tool schema for a calculator function
def get_calculator_schema():
return {
"name": "calculate",
"description": "Perform mathematical calculations with arbitrary precision",
"parameters": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "Mathematical expression to evaluate"
}
},
"required": ["expression"]
}
}
# Example tool schema for a search function
def get_search_schema():
return {
"name": "web_search",
"description": "Search the web for current information",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query"
}
},
"required": ["query"]
}
}
The model reads these schemas at inference time and decides which tool matches the current reasoning need.
Tool Selection Reasoning
The model must decide which tool to use. This requires both availability awareness and selection reasoning:
SYSTEM_PROMPT = """You have access to these tools:
- calculate(expression): Evaluate mathematical expressions
- web_search(query): Search for current information
- get_current_time(): Get current date and time
Decide which tool (if any) to use based on your reasoning needs.
If you need computation, use calculate.
If you need information after your training cutoff, use web_search.
When uncertain, prefer verification over assumption.
"""
Common failure mode: The model calls tools that don't exist because the schema was malformed or the model misunderstood capabilities. Always provide schema examples during inference.
Implementing Tool Calling
def call_tool(tool_name, parameters):
"""Execute a tool and return results"""
tool_registry = {
'calculate': calculate,
'eval_math': calculate, # Aliases help
'web_search': web_search,
'search': web_search,
'get_current_time': get_current_time
}
if tool_name not in tool_registry:
return {"error": f"Unknown tool: {tool_name}"}
tool_func = tool_registry[tool_name]
try:
result = tool_func(**parameters)
return {"success": True, "result": result}
except Exception as e:
return {"success": False, "error": str(e)}
Handling Tool Errors
Tool execution fails. The model must reason past failures:
- Network errors: Retry with exponential backoff, then gracefully degrade
- Invalid parameters: Log the attempted call, return error to model, let model reformulate
- Tool unavailable: Skip to alternative approach or inform user
A common failure pattern is models that treat tool errors as final. "The search failed" should trigger "try alternative search terms" not "I cannot answer."
def execute_with_fallback(tool_name, parameters, max_retries=2):
for attempt in range(max_retries + 1):
result = call_tool(tool_name, parameters)
if result.get('success'):
return result
# Exponential backoff for transient errors
if attempt < max_retries:
wait_time = 2 ** attempt
time.sleep(wait_time)
return {"error": "max_retries_exceeded", "tool": tool_name}
Real-Time Data Integration
For applications requiring current data (prices, weather, schedules), tool integration is necessary:
def stock_price_inquiry(symbol):
return web_search(f"{symbol} stock price today")
def weather_inquiry(location):
return web_search(f"weather forecast {location}")
When combined with reasoning, the model can answer complex queries: "Should I buy TechCorp if the meeting is tomorrow and there's a storm forecast for their headquarters?"
Define three tools for your application domain. Write tool schemas with descriptions specific enough for a reasoning model to know when to use each. Test by prompting the model to decide which tool to use for 10 sample queries.