New Tool Radar

AI Concepts

How to Let an Agent Query a Database Through Function Calling

Give an agent a database query function it can call, so it answers data questions from live rows instead of from memory.

Schematic illustration with three step boxes: define one query function, let the model choose, and run and return the rows.

Function calling is useful when you are building an application that bridges the model and the functionality of your application: you give the model access to functions that query a database, and it calls them to help the user. The model decides when to call and what arguments to pass; your code runs the function and returns the result.

Before you start

A database you can query with a read-only account, and an API client that supports function calling.

Define one narrow query function

Define a single function, such as lookup_order, with typed parameters for the order number and nothing else. A narrow function is easy for the model to fill correctly and easy for you to secure.

Describe each parameter in plain words, including the format: order numbers look like ORD-4821, not like a free-text search. Give the function a name the model can read.

Let the model choose when to call it

Send the function definition with the user question and let the model decide whether to call it. Do not force the call when the question may not need data: a forced call on a greeting wastes a round trip.

If the model calls the function with a bad argument, return an error message as the tool result and let it try again with a corrected argument.

Run the query and return the rows

Run the query with a read-only database account and return only the rows the user asked for. Never let the function run arbitrary SQL from the model: the function takes typed arguments and builds the query itself.

Check the returned rows before they reach the model: an empty result should become a clear statement that no order matched, not a guessed answer.

Practice check: fictional Maple Corner orders, 10 questions

The table below is fictional practice material, worked out by hand from the stated function calls. It is the expected result for the practice questions, not a run of any API.

Question Function called Rows returned Verdict
Where is order ORD-4821? lookup_order 1 row Pass
What is my order status? lookup_order 0 rows, asked for number Pass – asked for the number
Refund policy? None No call needed Pass
Order ORD-9999 status? lookup_order 0 rows Pass – no match stated

All four questions are handled correctly: data questions call the function, the policy question does not, and a missing order is stated as missing rather than invented.

Expected result. UNTESTED: the calls in the practice table are worked out by hand, not measured by running an API. Define one narrow query function, send it with ten real questions, and check that the model calls it only for data questions.

Limits

  • The model chooses the function and fills the arguments, but your code owns the query: never pass model text into SQL directly.
  • Function calling needs a read-only database account; a write account behind a model call is a risk, not a feature.

Sources

See all