AICredits logo
API

Tool Calling

Give LLMs access to your functions. Define tools, receive structured function call arguments, execute them, and return results.

Use this page with an AI assistant

Opens a new chat with this docs URL and the correct AICredits base URLs.

Tool calling (also called function calling) lets LLMs invoke functions in your code. You define the functions as JSON schemas, the model decides when to call them and with what arguments, you execute the functions, and return the results for the model to use in its final answer.

How It Works

  1. Send a request with a tools array describing available functions
  2. The model returns a response with finish_reason: "tool_calls" and a list of tool_calls
  3. Execute the functions with the provided arguments
  4. Send the results back as role: "tool" messages
  5. The model generates a final response using the tool results

Defining Tools

Tools are described using JSON Schema. Each tool has a name, description, and parameters schema:

Define tools
from openai import OpenAI

client = OpenAI(
    base_url="https://api.aicredits.in/v1",
    api_key="sk-your-key-here",
)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_stock_price",
            "description": "Get the current stock price for a ticker on a given exchange",
            "parameters": {
                "type": "object",
                "properties": {
                    "ticker": {
                        "type": "string",
                        "description": "The stock ticker symbol (e.g. RELIANCE, TCS)",
                    },
                    "exchange": {
                        "type": "string",
                        "enum": ["NSE", "BSE"],
                        "description": "The stock exchange",
                    },
                },
                "required": ["ticker", "exchange"],
            },
        },
    },
]

Full Tool Call Loop

import json
from openai import OpenAI

client = OpenAI(
    base_url="https://api.aicredits.in/v1",
    api_key="sk-your-key-here",
)

messages = [{"role": "user", "content": "What's the stock price of Reliance Industries?"}]

# Step 1: Initial request
response = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    messages=messages,
    tools=tools,
)

assistant_message = response.choices[0].message
messages.append(assistant_message)

# Step 2: Execute the tool
if assistant_message.tool_calls:
    for tool_call in assistant_message.tool_calls:
        args = json.loads(tool_call.function.arguments)

        if tool_call.function.name == "get_stock_price":
            result = {"price": 2847.50, "currency": "INR", "change": "+1.2%"}
        else:
            result = {"error": "Unknown function"}

        # Step 3: Add tool result to messages
        messages.append({
            "role": "tool",
            "tool_call_id": tool_call.id,
            "content": json.dumps(result),
        })

# Step 4: Get final response
final_response = client.chat.completions.create(
    model="openai/gpt-4o-mini",
    messages=messages,
    tools=tools,
)

print(final_response.choices[0].message.content)
# → "The current stock price of Reliance Industries (RELIANCE) is ₹2,847.50, up 1.2% today."
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.aicredits.in/v1",
  apiKey: "sk-your-key-here",
});

const messages: OpenAI.ChatCompletionMessageParam[] = [
  { role: "user", content: "What's the stock price of Reliance Industries?" },
];

// Step 1: Initial request
const response = await client.chat.completions.create({
  model: "openai/gpt-4o-mini",
  messages,
  tools,
});

const assistantMessage = response.choices[0].message;
messages.push(assistantMessage);

// Step 2: Execute tools
if (assistantMessage.tool_calls) {
  for (const toolCall of assistantMessage.tool_calls) {
    const args = JSON.parse(toolCall.function.arguments);
    const result = { price: 2847.50, currency: "INR", change: "+1.2%" };

    messages.push({
      role: "tool",
      tool_call_id: toolCall.id,
      content: JSON.stringify(result),
    });
  }
}

// Step 3: Get final response
const finalResponse = await client.chat.completions.create({
  model: "openai/gpt-4o-mini",
  messages,
});

console.log(finalResponse.choices[0].message.content);

Parallel Tool Calls

Models can request multiple tool calls in a single response. Execute them in parallel for best performance:

Parallel tool calls
import asyncio
import json
from openai import AsyncOpenAI

client = AsyncOpenAI(
    base_url="https://api.aicredits.in/v1",
    api_key="sk-your-key-here",
)

async def execute_tool(tool_call):
    args = json.loads(tool_call.function.arguments)
    if tool_call.function.name == "get_stock_price":
        return {"ticker": args["ticker"], "price": 1234.56}
    return {}

async def main():
    messages = [{"role": "user", "content": "Compare prices of TCS and Infosys"}]

    response = await client.chat.completions.create(
        model="openai/gpt-4o-mini",
        messages=messages,
        tools=tools,
    )

    tool_calls = response.choices[0].message.tool_calls
    messages.append(response.choices[0].message)

    # Execute all tool calls in parallel
    results = await asyncio.gather(*[execute_tool(tc) for tc in tool_calls])

    for tool_call, result in zip(tool_calls, results):
        messages.append({
            "role": "tool",
            "tool_call_id": tool_call.id,
            "content": json.dumps(result),
        })

    final = await client.chat.completions.create(
        model="openai/gpt-4o-mini",
        messages=messages,
    )
    print(final.choices[0].message.content)

asyncio.run(main())

Tool Choice

Control which tools the model can use with the tool_choice parameter:

ValueBehavior
"auto"Model decides whether to call a tool (default)
"none"Model will not call any tool
"required"Model must call at least one tool
{"type": "function", "function": {"name": "my_fn"}}Force a specific tool to be called

Provider Support

Tool calling is supported across all major providers. For providers that use a different native format (e.g., Anthropic's tool use), AICredits translates the request and response automatically:

ProviderModelsTool Calling
OpenAIGPT-4o, GPT-4o-mini, o1, o3Full support
AnthropicClaude Sonnet, Claude HaikuFull support
GoogleGemini 2.0 Flash, Gemini 1.5 ProFull support
DeepSeekDeepSeek Chat, DeepSeek R1Full support
MistralMistral Large, Mistral SmallFull support
xAIGrok BetaFull support

The OpenAI SDK handles response parsing for you. Regardless of which provider serves the request, message.tool_calls will always be in the OpenAI format.

On this page