← Back to Dashboard
1. Function and API Tool Patterns2. Retries, Idempotency, and Timeouts3. Web Search: Built-In Server-Side Grounding
Function and API Tool Patterns
📚 Tool Use and Orchestration⏱ 10 min⭐ 85 XP
Tool Contracts for Reliability
Tool calling should use strict schemas, deterministic validation, and safe fallbacks. The model proposes; your code disposes. Never let unvalidated model output call privileged systems directly.
Defining Tools in the Converse API
toolConfig={
"tools": [{
"toolSpec": {
"name": "create_incident",
"description": "Open an incident ticket for a degraded service",
"inputSchema": {"json": {
"type": "object",
"properties": {
"service": {"type": "string", "enum": ["checkout", "search", "auth"]},
"severity": {"type": "string", "enum": ["P1", "P2", "P3"]},
"summary": {"type": "string", "maxLength": 200}
},
"required": ["service", "severity", "summary"]
}}
}
}]
}
When the model responds with stopReason: "tool_use", you receive a structured toolUse block. Your runtime then validates, executes, and returns a toolResult message - the model never touches your systems directly.
The Validation Gauntlet (Every Call, Every Time)
- Schema validation - does the input parse against the declared JSON Schema? Enums respected, lengths capped?
- Policy check - is this tool allowed for this user/tenant/agent state? (e.g. P1 creation may require prior diagnosis step)
- Semantic sanity - does the referenced entity exist? Is "checkout" actually degraded per monitoring?
- Execution with authorization - the tool runs under your scoped credentials, never model-supplied ones.
- Structured result back - return outcome or a typed error the model can reason about.
Contract Contents Checklist
- Input and output schema (the model needs to know what comes back).
- Failure behavior - what the model should do on
NOT_FOUNDvsPERMISSION_DENIEDvsTIMEOUT. - Side-effect classification - read-only tools can auto-run; mutating tools may need approval gates.
Security rule: treat every tool argument as untrusted user input - because transitively, it is. Prompt-injected content can steer the model's tool arguments. Schema enums, allowlists, and server-side authorization are your real boundary.
🧪 Knowledge Check
Press 1-4 to select1 of 2
Best practice before tool execution is:
Blind execution
Schema validation and policy checks
Disable logging
Skip retries