Clark Farming CompanySoftware Foundry

AI / Agent Reference · Reference

Function Calling

Function calling enables Large Language Models to interact with external tools and APIs by generating structured function calls instead of natural language responses. The model receives function schemas, determines when and how to call them, and returns structured arguments for execution. Define functions: Provide function schemas (name, description, parameters) with the prompt Model decides: Large Language Model det…

draftneeds-review0 source links4 resolved links
wiki/wiki/ai-ml/function-calling.md

Answer

Function calling enables Large Language Models to interact with external tools and APIs by generating structured function calls instead of natural language responses. The model receives function schemas, determines when and how to call them, and returns structured arguments for execution. Define functions: Provide function schemas (name, description, parameters) with the prompt Model decides: Large Language Model det…

Auto-generated neutral summary from the source page — needs human review before trusted use.

Evidence & Source Cards

No explicit artifact, library, or external source links found in this sample slice. Evidence state remains needs-review.

Source Excerpt

Function calling enables Large Language Models to interact with external tools and APIs by generating structured function calls instead of natural language responses. The model receives function schemas, determines when and how to call them, and returns structured arguments for execution.

How It Works

Flow

User prompt → Large Language Model → Function call (name + args) → Application executes → Result → Large Language Model → Final response
  1. Define functions: Provide function schemas (name, description, parameters) with the prompt
  2. Model decides: Large Language Model determines if a function call is needed based on the prompt
  3. Generate arguments: Model produces structured JSON arguments
  4. Execute: Application runs the function with the provided arguments
  5. Return result: Function result is sent back to the model
  6. Final response: Model incorporates the result into its response

Function Schema (OpenAI Format)

{
  "name": "get_weather",
  "description": "Get current weather for a location",
  "parameters": {
    "type": "object",
    "properties": {
      "location": {
        "type": "string",
        "description": "City and state, e.g. San Francisco, CA"
      },
      "unit": {
        "type": "string",
        "enum": ["celsius", "fahrenheit"],
        "description": "Temperature unit"
      }
    },
    "required": ["location"]
  }
}

Model Response

{
  "id": "call_abc123",
  "type": "function",
  "function": {
    "name": "get_weather",
    "arguments": "{\"location\":\"New York, NY\",\"unit\":\"fahrenheit\"}"
  }
}

Provider Implementations

OpenAI

Anthropic (Claude)

Google (Gemini)

Best Practices

Schema Design

Error Handling

Multiple Functions

Common Patterns

Retrieval-Augmented Generation (RAG)

Function: search_knowledge_base(query: string) → list of documents
Use: Ground Large Language Model responses in verified knowledge

Database Queries

Function: query_database(sql: string) → list of rows
Use: Dynamic data retrieval from structured databases
Caution: Validate SQL, use parameterized queries, limit result size

API Integration

Function: call_external_api(endpoint: string, method: string, body: object) → response
Use: Integrate with external services (payment, shipping, weather)

Multi-Step Workflows

Step 1: search_products(category: string) → product list
Step 2: get_product_details(product_id: string) → details
Step 3: check_inventory(product_id: string) → stock level
Step 4: Final response with synthesized information

Security Considerations

Limitations

Related

Relationships

Outbound links

Referenced by

Tags

ai-mlfunction-callingtoolsllmapischema