So far, our AiLearningApi can accept a user's question and return an AI-generated response.
But there is one important limitation.
The AI model can generate text, but how can it interact with our actual application?
For example, imagine a user asks:
"What is the available inventory for material DP-100?"
The AI model doesn't automatically have access to our SQL Server database or business logic.
We need a mechanism that allows the AI model to request the execution of our C# methods.
This is called Function Calling or Tool Calling.
Today's objective is to understand this concept and implement it in our .NET 10 AiLearningApi project.
Function calling allows an AI model to identify when it needs an external function or tool to answer a user's question.
The model can request that your application execute a particular function with specified arguments.
Your application executes the function and sends the result back to the model.
The model then uses that result to formulate its final answer.
For example:
User asks:
"What is the available inventory for material DP-100?"
The AI model determines that it needs inventory information.
It requests:
{
"name": "GetMaterialInventory",
"arguments": {
"materialNumber": "DP-100"
}
}
Your .NET application executes the corresponding C# method, retrieves the inventory, and returns the result to the model.
The model might then answer:
"Material DP-100 currently has 250 units available."
Important: The AI model does not execute your C# method directly. It requests a tool call. Your application is responsible for executing it.
2. Understand the complete workflow
This is the fundamental function-calling workflow.
You will often hear both terms.
Function calling: The model requests that a particular function be called.
Tool calling: A broader term for the model requesting an external capability, such as a function, database lookup, search operation, or another service.
In modern AI APIs, these terms are often used interchangeably.
For our learning project, we'll implement tools using C# methods.
Consider these two scenarios.
Scenario A — Without function calling
User:
"What is the inventory for DP-100?"
AI:
"I don't have access to your inventory database. Please provide the inventory details."
The model can only work with information supplied to it.
Scenario B — With function calling
User:
"What is the inventory for DP-100?"
AI requests the inventory tool.
.NET application executes the tool and retrieves the database result.
AI:
"There are 250 units available."
This is how AI becomes useful in business applications.
We provide the model with tool definitions.
A tool definition describes:
Function name
Function purpose
Parameters
Parameter data types
Required parameters
For example:
{
"type": "function",
"function": {
"name": "GetMaterialInventory",
"description": "Gets available inventory for a material.",
"parameters": {
"type": "object",
"properties": {
"materialNumber": {
"type": "string",
"description": "The material number."
}
},
"required": ["materialNumber"]
}
}
}
This definition tells the model what the tool does and how it can request its use.
It does not give the model direct database access.
Suppose a user asks an AI assistant:
What is the available inventory for material DP-100?
The AI model doesn't have direct access to your database. It cannot independently execute your C# methods.
However, it can identify that it needs inventory information and request a function call.
The application must then:
Send the user's question to the AI model.
Receive the model's response.
Check whether the model requested a function call.
Execute the requested C# function.
Send the function result back to the AI model.
Receive and display the final answer.
This process is called the Manual Tool Calling Loop.
A few important concepts:
Tool definition: Describes the function and its parameters to the model.
Tool call: The model's request to execute a particular function.
Tool execution: Your application invokes the corresponding C# method.
Tool result: The output returned to the model.
Loop: Repeats until the model produces a normal final response or another stopping condition is reached.
A model may request multiple tools, or request another tool after receiving the first result. This is why the loop matters.
AutoInvoke allows the .NET application to automatically execute registered functions when the AI model requests them.
Instead of manually:
Inspecting tool calls.
Identifying the requested function.
Executing the C# method.
Returning the result to the model.
Repeating the process.
You configure the AI client with function-invocation middleware, and the framework manages much of this orchestration.
Important: AutoInvoke does not mean the AI model executes your C# code. Your .NET application still executes the function.
Manual Loop AutoInvoke
You inspect tool calls Framework handles invocation
You execute functions Framework invokes registered functions
You return tool results Framework manages the tool cycle
More control Less orchestration code
Manual Loop is particularly useful when you need custom execution policies, auditing, approval workflows, or specialized error handling.
Consideration Manual Loop AutoInvoke
Execution control Full application control Framework-managed
Implementation complexity Higher Lower
Custom execution policies Easier to implement directly Requires middleware or surrounding application logic
Human approval Explicitly controlled by application Must be designed into the application
Tool execution Application code Middleware invokes application code
So far, we've given our AI assistant one function. But real-world AI applications often need multiple capabilities.
For example, imagine building an AI assistant that can:
Retrieve product inventory.
Retrieve product details.
Check purchase-order status.
Calculate order totals.
Search for suppliers.
Instead of building a separate AI endpoint for every operation, we can expose multiple tools to the same AI assistant.
This is called Multi-tool Calling.
1. What is Multi-tool Calling?
Multi-tool calling means providing an AI model with multiple available functions so it can select the appropriate function based on the user's request.
Consider these questions:
User question Appropriate tool
What is the inventory of DP-100? GetMaterialInventory
Show me the details of DP-100. GetMaterialDetails
What is the status of PO-1001? GetPurchaseOrderStatus
Find suppliers for DP-100. SearchSuppliers
The AI model determines which tool or tools are relevant.
Important: The model selects and requests tools. Your application executes them.
2. How does Multi-tool Calling work?
The model may request multiple tools in one response. It may also request tools in successive rounds when the result of one operation is needed before choosing the next.
There are two important execution patterns.
Suppose the user asks: Give me the details and inventory of DP-100.
Both functions can independently execute because they don't depend on each other's results.
GetMaterialDetails("DP-100")
|
|---- Execute independently
|
GetMaterialInventory("DP-100")
Suppose the user asks:
Find the supplier with the lowest price for DP-100 and check whether that supplier has available stock.
This may require:
Search for suppliers and their prices.
Select the supplier with the lowest price.
Check stock for that selected supplier.
SearchSuppliers("DP-100")
|
v
SelectLowestPriceSupplier()
|
v
CheckSupplierStock()
|
v
Final Response
The model can request the second operation only after receiving the first operation's result.
Important distinction: Providing multiple tools does not automatically guarantee parallel execution. Actual execution behavior depends on the model, provider, and function-invocation implementation. For independent operations, you can also implement controlled parallel execution in your application.
When implementing multiple tools in a real application:
Give each tool a clear name and description.
Use strongly typed parameters wherever possible.
Validate all arguments before execution.
Apply user authorization independently to every tool.
Limit the number of tool calls and execution time.
Log tool names, execution outcomes, and relevant audit information.
Do not expose sensitive internal data in tool results.
For example, a user authorized to view inventory might not be authorized to modify inventory. Tool availability is not permission.
So far, we've learned how an AI assistant can automatically invoke C# functions using AutoInvoke and Multi-tool Calling.
But there is an important problem.
Should an AI assistant be allowed to execute every function automatically?
Consider these requests:
Check available inventory.
Retrieve material details.
Create a purchase order.
Update inventory quantities.
Delete a material record.
Transfer funds between accounts.
The first two are read operations. The remaining operations can change business data or cause financial consequences.
We shouldn't allow an AI assistant to execute sensitive operations without appropriate controls.
This is where Human Approval comes into the picture.
Human approval is a mechanism where an application pauses a requested operation until an authorized person explicitly approves it.
The AI model can recommend or request an action, but the application waits for approval before executing it.
For example:
User:
Update the inventory of DP-100 to 500 units.
AI model:
I need to execute UpdateMaterialInventory with these parameters:
Material Number: DP-100
New Quantity: 500
Application:
Approval required. Do you want to update the inventory?
Human:
Approve.
Application:
Executes the C# method.
The key principle is:
AI proposes the action. The application enforces the approval process.
2. How does the approval workflow work?
A useful starting point is to classify tools by their impact.
Operation Suggested approval policy
Get material details No approval
Get inventory No approval
Search suppliers No approval
Create purchase order Approval
Update inventory Approval
Delete material Approval
Transfer funds Strong approval controls
This is not a universal rule. The application owner should define approval requirements based on business risk, user permissions, and regulatory requirements.
There is an important architectural detail here.
Imagine a user requests an inventory update and then closes the browser.
The approval request should not disappear.
A production application should persist something similar to:
Field Example
ApprovalId 1001
RequestedBy User123
ToolName UpdateMaterialInventory
MaterialNumber DP-100
NewQuantity 500
Status Pending
ApprovedBy NULL
CreatedAt UTC timestamp
Possible statuses:
Pending
Approved
Rejected
Expired
Executed
ExecutionFailed
The application should also prevent duplicate execution and ensure that the approved parameters are exactly the parameters that get executed.
For example, don't approve an update to 500 units and then allow the execution request to change it to 1,000 units.
A secure implementation should use an immutable approval record, revalidate authorization and business rules at execution time, and protect against duplicate or replayed approvals.
Human approval is not simply displaying an Approve button.
A robust implementation should consider:
Authentication: Identify who is requesting the action.
Authorization: Verify that the approver has permission.
Parameter integrity: Ensure approved parameters cannot be changed afterward.
Expiration: Automatically expire old approval requests.
Audit logging: Record who requested, approved, rejected, and executed the operation.
Idempotency: Prevent duplicate execution if the approval action is submitted more than once.
Also, never treat a model-generated statement such as "the user approved this" as actual approval. Approval must come through a trusted application-controlled mechanism.