Patterns
๐Ÿ“ž

Function Calling

Structured interface for AI to invoke external functions and APIs

Complexity: mediumTool Use

๐ŸŽฏ 30-Second Overview

Pattern: Enable AI to invoke external functions and APIs through structured schemas

Why: Breaks LLM knowledge limitations, enables real-time data access, external actions, and precise calculations

Key Insight: Schema-driven function calling with parameter validation, parallel execution, and security controls

โšก Quick Implementation

1Define Schema:JSON schema with parameters & types
2Register Tools:Add to function registry with auth
3Validate Params:Sanitize & validate all inputs
4Execute:Call API with timeouts & retries
5Process Results:Normalize & integrate responses
Example: get_weather(location="NYC", units="celsius") โ†’ {"temp": 18, "condition": "cloudy"}

๐Ÿ“‹ Do's & Don'ts

โœ…Write detailed function descriptions (3-4+ sentences minimum)
โœ…Use strict JSON schemas with required/optional field validation
โœ…Implement parameter sanitization and injection attack prevention
โœ…Add timeouts, retries with exponential backoff, and circuit breakers
โœ…Use parallel execution for independent function calls
โœ…Cache deterministic function results for cost optimization
โŒUse vague function descriptions that lead to hallucinated parameters
โŒAllow unbounded parallel calls without rate limiting
โŒRetry non-idempotent operations after timeouts
โŒExpose raw API credentials or secrets in function calls
โŒSkip input validation and security scanning

๐Ÿšฆ When to Use

Use When

  • โ€ข Need real-time data beyond model training cutoff
  • โ€ข Require external API integrations or database queries
  • โ€ข Must perform precise calculations or code execution
  • โ€ข Triggering actions in external systems (email, scheduling)

Avoid When

  • โ€ข Information available in model knowledge with acceptable quality
  • โ€ข Hard real-time requirements where tool overhead breaks SLOs
  • โ€ข High-risk irreversible actions without human approval
  • โ€ข Simple tasks solvable with basic text generation

๐Ÿ“Š Key Metrics

Function Selection Accuracy
% correct tool chosen from registry
Parameter Extraction Rate
% required params correctly identified
Schema Validation Success
% calls passing validation checks
API Success Rate
% successful external function calls
Parallel Execution Efficiency
Latency reduction vs sequential
Token Usage Optimization
Context tokens per function call
Security Incident Rate
Injection/XSS attempts blocked
Cost per Task
Total cost: model + API + infrastructure

๐Ÿ’ก Top Use Cases

Weather & Real-time Data: get_weather("NYC") โ†’ current conditions for decision making
Calendar Integration: schedule_meeting(time, attendees) โ†’ automated scheduling workflows
Database Operations: query_db(table, filters) โ†’ dynamic data retrieval and updates
Code Execution: run_python(code) โ†’ computational tasks and data analysis
Email & Communications: send_email(to, subject, body) โ†’ automated notifications
Web Search: search_web(query) โ†’ real-time information retrieval
File Operations: read_file(path), write_file(path, content) โ†’ document processing
API Integrations: call_api(endpoint, params) โ†’ external service interactions

References & Further Reading

Deepen your understanding with these curated resources

Contribute to this collection

Know a great resource? Submit a pull request to add it.

Contribute

Patterns

closed

Loading...

Built by Kortexya