

runWithTools, but if you call it with Responses API format, you will get back tool calls. e.g. similar to this: https://github.com/cloudflare/sandbox-sdk/blob/main/examples/code-interpreter/src/index.ts#L26-L37const messages = [ { role: "user", content: "How are you?" } ];
const tools = AITools;
const result = await AI.run("@cf/meta/llama-4-scout-17b-16e-instruct", {
messages,
tools,
max_tokens: 512
});/**
*
* @type {import("@cloudflare/workers-types").Ai_Cf_Meta_Llama_4_Messages["tools"]}
*/
export const AITools = [
{
name: "getClassRoster",
description: "Returns all student objects for that class",
parameters: {
type: "object",
properties: {
grade: {
type: "number",
description: "The grade (e.g. 10) of the needed class. Between 1 and 12, inclusive."
},
classLetter: {
type: "string",
description: "The letter (e.g. A) of the needed class. Between A and Z, inclusive."
}
},
required: ["grade", "classLetter"]
}
}
]export const AITools: Ai_Cf_Meta_Llama_4_Messages["tools"] = [
{
type: "function",
function: {
name: "getClassRoster",
description: "Returns all student objects for that class",
parameters: {
type: "object",
properties: {
grade: {
type: "number",
description: "The grade (e.g. 10) of the needed class. Between 1 and 12, inclusive."
},
classLetter: {
type: "string",
description: "The letter (e.g. A) of the needed class. Between A and Z, inclusive."
}
},
required: ["grade", "classLetter"]
}
}
}
][
{
"role": "user",
"content": "Who are the students of class 10/A"
},
{
"role": "tool",
"name": "getClassRoster",
"content": "[{\"englishName\":\"Aaser Mohammed Ahmed\",\"arabicName\":\"آسر Ù…ØÙ…د Ø£ØÙ…د\",\"gender\":\"Male\",\"religion\":\"Muslim\",\"studentId\":\"0123456789\"}]"
},
{
"role": "assistant",
"content": "The students of class 10/A are:\n\n* Aaser Mohammed Ahmed \n\nWould you like to know more about their attendance?"
}
]export const AITools = [{
type: "function",
function: {
name: "getClassRoster",
description: "Returns all student objects for that class. If you get students with 'Student {x}' English names, then use their Arabic names.",
parameters: {
type: "object",
properties: {
grade: {
type: "number",
description: "The grade (e.g. 10) of the needed class. Between 1 and 12, inclusive."
},
classLetter: {
type: "string",
description: "The letter (e.g. A) of the needed class. Between A and Z, inclusive."
}
},
required: ["grade", "classLetter"]
}
}
}, {
type: "function",
function: {
name: "getAttendanceForDate",
description: "Returns attendance for all periods on a given day for all classes. You would need the required class roster(s) first to know which students are in which classes.",
parameters: {
type: "object",
properties: {
date: {
type: "string",
description: "The date to get attendance for. Must be in the format DD/MM/YYYY."
}
},
required: ["date"]
}
}
}]Initial AI result: {
"response": "To answer your question, I need to follow these steps:\n\n1. Get the roster for class 10/A.\n2. Get attendance for class 10/A on the 1st of September, 2025.\n3. Get attendance for class 10/A on the 31st of August, 2025.\n\nFirst, I will get the roster for class 10/A. \n{\"name\": \"getClassRoster\", \"parameters\": {\"grade\": 10, \"classLetter\": \"A\"}}\n\nThen, I will get attendance for the 1st of September, 2025. \n{\"name\": \"getAttendanceForDate\", \"parameters\": {\"date\": \"01/09/2025\"}}\n\nLastly, I will get attendance for the 31st of August, 2025. \n{\"name\": \"getAttendanceForDate\", \"parameters\": {\"date\": \"31/08/2025\"}}",
"tool_calls": [],
"usage": {
"prompt_tokens": 646,
"completion_tokens": 191,
"total_tokens": 837
}
}[
{
"role": "system",
"content": "System prompt goes here"
},
{
"role": "user",
"content": "What's ابراهيم Ù…ØÙ…د ابراهيم الدسوقي's attendance percentage this year in class 10/A?"
},
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"type": "function",
"function": {
"name": "getClassRoster",
"arguments": "{\"grade\":10,\"classLetter\":\"A\"}"
},
"id": "000000001"
}
]
},
{
"role": "tool",
"name": "getClassRoster",
"content": "{ ...stuffHere }"
}
][wrangler:error] AiError: 3043: Internal server error
at Ai._parseError (cloudflare-internal:ai-api:212:24)
at async Ai.run (cloudflare-internal:ai-api:190:19)
at async runWorker (file:///home/proman4713/JavaScript%20Projects/MawjoodAPI/src/index.js:50:20)
at async jsonError (file:///home/proman4713/.nvm/versions/node/v22.13.1/lib/node_modules/wrangler/templates/middleware/middleware-miniflare3-json-error.ts:22:10)
at async drainBody (file:///home/proman4713/.nvm/versions/node/v22.13.1/lib/node_modules/wrangler/templates/middleware/middleware-ensure-req-body-drained.ts:5:10)messages: [
{
"role": "system",
"content": "sys prompt goes here"
},
{
"role": "user",
"content": "What's ابراهيم Ù…ØÙ…د ابراهيم الدسوقي's attendance percentage this year in class 10/A?"
},
{
"role": "assistant",
"content": "{\"name\":\"getClassRoster\",\"arguments\":{\"grade\":10,\"classLetter\":\"A\"}}"
},
{
"role": "tool",
"content": "{\"data\":[{\"englishName\":\"bob\",\"arabicName\":\"ابراهيم Ù…ØÙ…د ابراهيم الدسوقي\",\"studentId\":123}]}",
"tool_call_id": "000000001"
}
]import { Hono } from "hono";
export const TOOLS = [
{
type: "function",
name: "get_weather",
description: "Get the weather for a given location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "The location to get the weather for",
},
},
required: ["location"],
additionalProperties: false,
},
output: '{"weather":"sunny"}',
}
]
// GPT OSS router
const gptOss = new Hono<{ Bindings: Env }>();
gptOss.get('/get', async (c) => {
const response = await c.env.AI.run("@cf/openai/gpt-oss-20b" as any, { input: "what is the weather like in London?", tools: TOOLS });
return c.json(response);
});
export default gptOss;