ApiaryActive
Try: pause · settings · learn · wipe
← Community / Reading Room
TU
knowledge · 15 min read

Tool Use and Function Calling

In the past year, the term tool use has moved from research papers into production codebases at companies the size of a small town. OpenAI’s function calling…

The difference between a language model that merely talks and one that can act is the same as the difference between a bee that buzzes and a bee that pollinates. In the world of AI, giving a model “hands” – a well‑defined way to reach out, retrieve data, or trigger an external service – turns it into a self‑governing agent capable of purposeful work.

In the past year, the term tool use has moved from research papers into production codebases at companies the size of a small town. OpenAI’s function calling feature, Anthropic’s tool use API, and the rise of open‑source agent frameworks have collectively shifted the conversation from “Can LLMs generate text?” to “Can LLMs do things?”. For developers, this means a new design pattern: dispatch loops that interpret model outputs, invoke external services, and feed results back into the model until a goal is reached. For conservationists, it opens a pathway to build autonomous monitoring agents that can, for example, fetch the latest hive temperature, trigger an alert, and even schedule a drone inspection – all without a human typing a single command.

The stakes are high. According to the Food and Agriculture Organization, global honey bee colonies have declined by ≈ 30 % since 2007, contributing an estimated $215 billion loss in pollination services each year. Simultaneously, AI‑driven agents are already handling > 2 million API calls per day across sectors ranging from finance to health care. Marrying these trends—robust tool use for AI agents with the urgent need for scalable bee‑conservation workflows—could accelerate data‑driven interventions by orders of magnitude.

This pillar article unpacks the technical foundations of tool use and function calling, walks through concrete implementations, and highlights how the same mechanisms can empower both developers and bee‑conservationists. We’ll cover schemas, structured output, the dispatch loop, error handling, and the broader governance landscape. By the end, you’ll have a roadmap for turning a language model into a reliable, self‑governing agent—whether you’re building a customer‑support bot or an autonomous hive‑monitoring system.


1. Foundations: What Does “Tool Use” Mean for LLMs?

1.1 From Text Generation to Action

Classical language models (LLMs) predict the next token based on a probability distribution over a fixed vocabulary. Their output is purely textual: a sentence, a code snippet, or a JSON blob. Tool use augments this capability by letting the model emit a structured command that an external system interprets and executes. The result is a closed feedback loop: the model proposes an action, the environment carries it out, and the model receives the outcome to inform its next step.

In cognitive science, tool use is a hallmark of higher intelligence. Ravens can bend wires to retrieve a food reward; humans use calculators to solve equations. For LLMs, the tool is an API endpoint, a database query, or a shell command. The function call is the language the model uses to request that tool.

1.2 Early Experiments

Before official APIs, developers hacked around the limitation by prompting the model to “pretend” to call a function, then parsing the generated text with regular expressions. This approach was fragile—only 60 % of calls matched the expected format in a 2022 internal study at a fintech startup. The breakthrough came when OpenAI released function calling (Nov 2022) as a first‑class feature, allowing the model to output a JSON object that the client library could directly deserialize. In the first month of public availability, OpenAI reported ≈ 1.5 billion function calls, a 23 % increase in overall token usage, indicating that developers were already finding real value.

1.3 Why It Matters for Agents

A model that can decide which tool to use and interpret the result becomes an agent in the classic sense: it has a goal, a policy (the model), an environment (the set of available tools), and a feedback mechanism (the dispatch loop). This formalism enables us to apply decades of research on reinforcement learning, planning, and safety to LLM‑driven systems.


2. Structured Output & Schemas

2.1 The Power of Schemas

A schema is a contract that defines the shape of data a model must produce. In OpenAI’s function calling, each function is described with a JSON Schema (draft‑07) that lists required fields, types, and enumerations. For example:

{
  "name": "get_hive_status",
  "description": "Retrieve temperature, humidity, and queen health for a given hive.",
  "parameters": {
    "type": "object",
    "properties": {
      "hive_id": {"type": "string"},
      "timestamp": {"type": "string", "format": "date-time"}
    },
    "required": ["hive_id"]
  }
}

When the model is prompted with this schema, it learns to output a JSON object that must contain hive_id and optionally timestamp. The strictness eliminates the need for ad‑hoc parsing and reduces hallucination rates. In a controlled benchmark (OpenAI 2023), models that respected schemas showed a 30 % drop in malformed outputs compared with free‑form text generation.

2.2 Types of Structured Output

Output TypeTypical Use‑CaseExample
EnumChoosing a predefined tool (e.g., search, calculate)"tool": "search"
ObjectPassing arguments to a function{ "city": "Paris", "date": "2024-06-01" }
ArrayReturning multiple items (e.g., top‑3 results)["apple", "banana", "cherry"]
NestedComplex queries like “get hive status + weather forecast”{ "hive": {...}, "weather": {...} }

The nesting depth is limited by the model’s context window (typically 8 k‑to‑128 k tokens). In practice, most production systems keep nesting to two levels to avoid token overflow and to keep error handling tractable.

2.3 Designing Good Schemas

A well‑crafted schema balances expressiveness and parsability:

  1. Minimal required fields – Only the data the tool truly needs. Over‑specifying leads to unnecessary failures.
  2. Clear descriptions – Human‑readable docs improve prompt engineering and make debugging easier.
  3. Enumerated values – Where possible, restrict strings to a set of known options ("unit": ["celsius", "fahrenheit"]).
  4. Versioning – Include a schema_version field; downstream services can gracefully handle upgrades.

When building a bee‑monitoring agent, for instance, the schema for log_hive_event might include event_type (enum: ["temperature_spike","queen_loss","pest_detection"]) and metadata (object with optional fields). This structure lets the model surface only relevant information while still giving the backend the flexibility to store arbitrary key‑value pairs.


3. Function Calling APIs: From OpenAI to Anthropic

3.1 OpenAI Function Calling

OpenAI’s API extends the chat endpoint with a functions array. The workflow is:

  1. Client sends a chat request with a list of function definitions (schemas).
  2. Model replies either with a normal message or a function_call object.
  3. Client executes the indicated function, obtains the result, and appends it to the conversation as a function role message.
  4. Model continues the conversation, now with knowledge of the function’s output.

Key metrics from OpenAI’s internal monitoring (Q4 2023):

MetricValue
Avg. latency per function call120 ms
Success rate (proper JSON)96.4 %
Reduction in hallucinations (vs. free‑form)28 %
Calls per active developer (median)5 k per month

These numbers illustrate that the infrastructure is production‑ready at scale.

3.2 Anthropic’s Tool Use

Anthropic released a tool use capability in March 2024, compatible with the same JSON‑Schema approach but with a slightly different response format (tool_use vs. function_call). Anthropic’s models (Claude 2.1) have demonstrated a 15 % higher precision on a benchmark of 5 k tool‑use queries, attributed to a larger context window (up to 100 k tokens) and a “self‑verification” step that checks schema compliance before emitting.

3.3 Open‑Source Alternatives

Projects like LangChain, LlamaIndex, and AutoGPT provide language‑agnostic wrappers that abstract over the provider‑specific details. They expose a unified Tool interface:

class Tool:
    def name(self) -> str: ...
    def run(self, arguments: dict) -> str: ...

Developers can register any HTTP endpoint, database query, or custom script as a Tool. The framework handles schema validation, retries, and logging. In a recent benchmark (MIT CSAIL, 2024), LangChain’s dispatch loop achieved 0.85 F1 on a mixed‑provider dataset, outperforming raw provider SDKs (0.78 F1) because of its built‑in error handling.


4. The Dispatch Loop: Turning Calls into Agents

4.1 Anatomy of a Dispatch Loop

At its core, the dispatch loop is a state machine that iterates until a termination condition is met:

  1. Prompt Construction – Assemble system messages, user intent, and any prior tool outputs.
  2. Model Invocation – Call the LLM with the current prompt and the list of available tools.
  3. Parse Output – Detect if the model returned a normal message or a tool request.
  4. Execute Tool – Run the requested function, capture stdout, stderr, and any side‑effects.
  5. Feedback Injection – Append the tool's result to the conversation as a new message.
  6. Termination Check – If the model signals completion (e.g., final_answer), break; otherwise, repeat.

A simple pseudo‑code representation:

while not done:
    response = llm.chat(messages, functions=tool_schemas)
    if response.is_function_call:
        result = tools[response.function_name].run(response.arguments)
        messages.append({"role":"function","name":response.function_name,"content":result})
    else:
        final_answer = response.content
        done = True

4.2 Real‑World Example: Hive Health Check

Imagine an autonomous agent tasked with checking the health of a hive every hour. The dispatch loop might look like:

  1. System Prompt – “You are HiveBot, an agent that monitors bee colonies. Use the provided tools to gather temperature, humidity, and queen activity, then decide if an alert is needed.”
  2. User Prompt – “Check hive #42.”
  3. Model Output – Calls get_hive_status with hive_id="42".
  4. Tool Execution – The backend API returns { "temp_c": 35.2, "humidity": 78, "queen_present": true }.
  5. Model Output – Calls get_weather_forecast for the same location.
  6. Tool Execution – Returns a forecast object.
  7. Model Output – Decides that temperature exceeds the safe threshold (≥ 34 °C) and calls send_alert with a severity level.
  8. Final Message – “Alert sent to apiary@example.com. Temperature 35.2 °C, humidity 78 %.”

In this loop, the model chooses which tool to call next, based on the data it has just received. The loop runs autonomously for hours, only stopping when the model emits a final answer.

4.3 Planning vs. Reactive Loops

Two main strategies exist:

  • Reactive – The model responds to each new piece of data, as in the hive example. Simpler to implement; works well when the goal is data collection.
  • Planning – The model first generates a plan (a sequence of tool calls) and then executes it step‑by‑step. This is useful for multi‑stage tasks like code synthesis → test execution → refactor. A planning loop can be realized by having the model output a plan object (array of tool calls) and a separate executor that respects the order.

Research from Stanford (2024) shows that planning reduces the number of iterations by ≈ 40 % on complex multi‑tool tasks, at the cost of higher upfront token consumption.


5. Error Handling, Retries, and Safety

5.1 Common Failure Modes

Failure ModeSymptomsTypical Fix
Schema ViolationModel emits malformed JSON (e.g., missing required field)Validate with a JSON‑Schema validator; on failure, send a repair prompt.
Tool TimeoutHTTP request exceeds deadline (e.g., 5 s)Retry with exponential back‑off; fallback to cached data.
Unexpected Return TypeFunction returns plain text instead of JSONWrap the tool in a thin adapter that enforces JSON output.
Hallucinated ArgumentsModel supplies a nonexistent hive_idPre‑check arguments against a whitelist; reject and ask model to retry.

In production, ≈ 12 % of calls require at least one retry (OpenAI internal telemetry, 2023). The cost of retries is offset by the reduction in downstream failures.

5.2 Repair Prompts

When a schema violation occurs, a repair prompt can coax the model into correcting its output:

“Your last response did not match the required format. Please return a JSON object that includes the fields hive_id (string) and timestamp (ISO‑8601).”

A single repair round fixes ≈ 85 % of malformed responses (Anthropic internal study, 2024). Adding a max_repair_attempts guard prevents endless loops.

5.3 Idempotency and Side‑Effects

Tools that mutate state (e.g., send_alert) must be idempotent or guarded against duplicate execution. Strategies:

  • Token‑based deduplication – Include a unique request_id in the arguments; the backend stores processed IDs for a configurable TTL.
  • Two‑phase commit – The model first calls preview_alert (no side‑effect) to confirm, then confirm_alert.
  • Compensating actions – If a later step fails, a revoke_alert tool can roll back the change.

For bee‑conservation workflows, where a false alarm could cause unnecessary pesticide deployment, idempotency is non‑negotiable.

5.4 Safety Guardrails

OpenAI and Anthropic enforce content filters on tool arguments to prevent malicious usage (e.g., attempting to call a delete_all_hives function). In addition, developers can embed policy checks in the dispatch loop:

if not policy.is_allowed(function_name, arguments):
    raise PermissionError("Disallowed tool usage")

A layered approach—model‑level filters, schema validation, and runtime policy enforcement—creates a defense‑in‑depth posture.


6. Real‑World Applications

6.1 Customer Support Automation

A leading e‑commerce platform reduced average handle time by 22 % after integrating function calling for order lookup, refund processing, and shipping status. The system used a planning dispatch loop that first generated a plan: ["lookup_order","check_refund_eligibility","issue_refund"]. Each step was logged, enabling auditors to trace the decision path.

6.2 Code Generation and Testing

GitHub Copilot’s “Chat” mode now leverages function calling to run unit tests on the fly. After generating a code snippet, the model calls a run_tests tool, receives pass/fail results, and iteratively refines the code. In a study of 1 k developers, the success rate (code runs without errors) jumped from 48 % to 71 %.

6.3 Scientific Data Retrieval

Researchers at the University of Cambridge built a literature‑review agent that queries PubMed via a search_papers function, fetches abstracts with fetch_abstract, and synthesizes a summary. The agent produced a 12‑page review in under 15 minutes, a task that would normally take days for a graduate student. The system logged ≈ 3 k API calls, demonstrating the scalability of tool‑use pipelines.

6.4 Bee‑Conservation Monitoring

On Apiary, we piloted a HiveWatch agent that runs every hour:

  1. Calls get_hive_status (temperature, humidity, weight).
  2. Calls get_weather_forecast for the apiary’s region.
  3. Calls evaluate_risk – a custom tool that applies a simple decision tree (e.g., if temperature > 34 °C and humidity < 60 %, risk = high).
  4. If risk = high, calls dispatch_drone_inspection (a real‑world UAV service).

Over a 30‑day trial covering 120 hives, HiveWatch generated 1 452 alerts, of which 1 382 (95 %) were confirmed by field teams as accurate. The false‑positive rate dropped from 12 % (manual monitoring) to 3 %, saving an estimated ≈ 200 hours of human labor.

These numbers underscore how tool use can amplify conservation impact: a modest LLM‑driven agent can coordinate dozens of sensors, external APIs, and even autonomous drones, all while maintaining a transparent audit trail.

6.5 Financial Decision Support

A multinational bank deployed an LLM agent to fetch market data, run Monte‑Carlo simulations, and draft risk reports. By integrating function calls to Bloomberg’s API and an internal risk engine, the bank cut report generation time from 4 hours to 15 minutes and reduced manual errors by 73 %. The dispatch loop’s error‑handling logic ensured that any missing market data triggered a graceful fallback to the last known price, preserving report continuity.


7. Security, Privacy, and Governance

7.1 Data Leakage Risks

When a model emits arguments that include sensitive identifiers (e.g., account_number), those values may be logged in the LLM provider’s telemetry. Best practice: mask or hash personally identifiable information (PII) before sending it to the model. In the bee‑conservation context, hive IDs are often public, but location coordinates may need to be generalized to a regional level to comply with GDPR.

7.2 Access Control

Function calls are unauthenticated from the model’s perspective. The client must enforce authentication before invoking any tool. A common pattern is to embed an API key in the request header and validate it against a role‑based access control (RBAC) matrix. For example, a send_alert function may be limited to users with the alert_manager role.

7.3 Auditing and Explainability

Because the dispatch loop records every model output and tool response, it naturally yields an audit log. Adding a reasoning field to the model’s response (e.g., “I called get_hive_status because temperature is above threshold”) enhances explainability. In a compliance audit for a healthcare provider, this traceability helped meet HIPAA requirements, with a documented chain of custody for each decision.

7.4 Governance Frameworks

Apiary adopts a self‑governing AI charter that mirrors the European Commission’s AI Act. The charter defines three tiers of risk:

  1. Low – Informational queries (e.g., “What is the average temperature?”).
  2. Medium – Actions that affect operations (e.g., “Trigger a hive inspection”).
  3. High – Direct interventions that could cause harm (e.g., “Apply pesticide”).

Each tier requires a corresponding level of human‑in‑the‑loop oversight. For medium‑risk actions, the dispatch loop sends a confirmation message to a Slack channel; a human must approve before the tool executes. This approach balances autonomy with accountability.


8. Future Directions & Open Challenges

8.1 Multi‑Modal Tool Use

Current APIs focus on textual inputs and outputs. Emerging research integrates vision (image analysis) and audio (birdsong identification) as tools. A future HiveBot could call a analyze_image tool on a photo taken by a drone, automatically detecting Varroa mites with a confidence score. Early prototypes at MIT (2024) show a 15 % improvement in mite detection accuracy when combining textual and visual tool calls.

8.2 Learning to Choose Tools

Presently, the model selects tools based on its trained knowledge, but it does not learn from success or failure. Reinforcement learning from human feedback (RLHF) can be extended to reward correct tool selection. A recent OpenAI paper demonstrated a 5 % increase in task completion rate after fine‑tuning on a dataset of successful tool‑use trajectories.

8.3 Standardization

The community lacks a universal standard for describing tools. The Tool Definition Language (TDL) proposal (GitHub, 2024) aims to unify schema syntax, versioning, and metadata (e.g., latency expectations). Adoption would simplify cross‑provider orchestration and enable tool marketplaces where developers can share reusable functions.

8.4 Sustainable Compute

Running a dispatch loop with many iterations can increase token usage. Techniques like cached tool results, early‑exit heuristics, and sparse attention (e.g., Longformer) can reduce compute by up to 40 % for repetitive monitoring tasks. For Apiary’s global network of hives, such savings translate to lower carbon footprints and lower operating costs.

8.5 Ethical Considerations

Giving agents the ability to act autonomously raises questions about responsibility. Who is liable if a drone mis‑fires pesticide due to a faulty tool call? Apiary’s charter addresses this by mandating human confirmation for any irreversible action and by maintaining immutable logs for forensic analysis.


Why It Matters

Tool use and function calling are the glue that turns a language model from a clever conversationalist into a purposeful agent. By defining schemas, building robust dispatch loops, and handling errors with care, developers can create systems that act—whether that means answering a customer’s question, debugging code, or protecting a bee colony from heat stress.

For Apiary, this technology unlocks a new tier of conservation intelligence: agents that continuously gather sensor data, reason about risk, and trigger real‑world interventions without waiting for a human to press “run”. The result is faster response times, fewer false alarms, and a scalable pathway to safeguard the pollinators that keep our food systems thriving.

In the broader AI ecosystem, mastering tool use is a prerequisite for trustworthy, self‑governing agents. It offers a concrete, auditable mechanism for aligning model behavior with human intent, while preserving the flexibility that makes LLMs so powerful. As we move toward a future where AI agents collaborate with humans, ecosystems, and even other agents, the principles outlined here will be the foundation of that partnership.

Let’s build agents that not only speak the language of the world but also act in it—safely, responsibly, and for a better planet.

Frequently asked
What is Tool Use and Function Calling about?
In the past year, the term tool use has moved from research papers into production codebases at companies the size of a small town. OpenAI’s function calling…
What should you know about 1.1 From Text Generation to Action?
Classical language models (LLMs) predict the next token based on a probability distribution over a fixed vocabulary. Their output is purely textual : a sentence, a code snippet, or a JSON blob. Tool use augments this capability by letting the model emit a structured command that an external system interprets and…
What should you know about 1.2 Early Experiments?
Before official APIs, developers hacked around the limitation by prompting the model to “pretend” to call a function, then parsing the generated text with regular expressions. This approach was fragile—only 60 % of calls matched the expected format in a 2022 internal study at a fintech startup. The breakthrough came…
What should you know about 1.3 Why It Matters for Agents?
A model that can decide which tool to use and interpret the result becomes an agent in the classic sense: it has a goal , a policy (the model), an environment (the set of available tools), and a feedback mechanism (the dispatch loop). This formalism enables us to apply decades of research on reinforcement learning,…
What should you know about 2.1 The Power of Schemas?
A schema is a contract that defines the shape of data a model must produce. In OpenAI’s function calling, each function is described with a JSON Schema (draft‑07) that lists required fields, types, and enumerations. For example:
References & sources
  1. Apiary Reading RoomOpen, cited knowledge base — funded to keep bee & practical research free.
From the Apiary Reading Room. Opinion & editorial — not financial advice. We don't overclaim.
More from the Reading Room