Get Started (3)

Last updated Apr 21, 2026 This guide will instruct you through setting up and deploying your first Workers AI project with embedded function calling. You will use Workers, a Workers AI binding, the ai-utils package ↗︎, and a large language model (LLM) to deploy your first AI-powered application on the Cloudflare global network with embedded function calling.

1. Create a Worker project with Workers AI

Follow the Workers AI Get Started Guide until step 2.

2. Install additional npm package

Next, run the following command in your project repository to install the Worker AI utilities package.

npmyarnpnpmbun

npm i @cloudflare/ai-utils
yarn add @cloudflare/ai-utils
pnpm add @cloudflare/ai-utils
bun add @cloudflare/ai-utils

3. Add Workers AI Embedded function calling

Update the index.ts file in your application directory with the following code:

index.jsjs

import { runWithTools } from "@cloudflare/ai-utils";

export default {
    async fetch(request, env, ctx) {
        // Define function
        const sum = (args) => {
            const { a, b } = args;
            return Promise.resolve((a + b).toString());
        };
        // Run AI inference with function calling
        const response = await runWithTools(
            env.AI,
            // Model with function calling support
            "@hf/nousresearch/hermes-2-pro-mistral-7b",
            {
                // Messages
                messages: [
                    {
                        role: "user",
                        content: "What the result of 123123123 + 10343030?",
                    },
                ],
                // Definition of available tools the AI model can leverage
                tools: [
                    {
                        name: "sum",
                        description: "Sum up two numbers and returns the result",
                        parameters: {
                            type: "object",
                            properties: {
                                a: { type: "number", description: "the first number" },
                                b: { type: "number", description: "the second number" },
                            },
                            required: ["a", "b"],
                        },
                        // reference to previously defined function
                        function: sum,
                    },
                ],
            },
        );
        return new Response(JSON.stringify(response));
    },
};

index.tsts

import { runWithTools } from "@cloudflare/ai-utils";

type Env = {
    AI: Ai;
};

export default {
    async fetch(request, env, ctx) {
        // Define function
        const sum = (args: { a: number; b: number }): Promise<string> => {
            const { a, b } = args;
            return Promise.resolve((a + b).toString());
        };
        // Run AI inference with function calling
        const response = await runWithTools(
            env.AI,
            // Model with function calling support
            "@hf/nousresearch/hermes-2-pro-mistral-7b",
            {
                // Messages
                messages: [
                    {
                        role: "user",
                        content: "What the result of 123123123 + 10343030?",
                    },
                ],
                // Definition of available tools the AI model can leverage
                tools: [
                    {
                        name: "sum",
                        description: "Sum up two numbers and returns the result",
                        parameters: {
                            type: "object",
                            properties: {
                                a: { type: "number", description: "the first number" },
                                b: { type: "number", description: "the second number" },
                            },
                            required: ["a", "b"],
                        },
                        // reference to previously defined function
                        function: sum,
                    },
                ],
            },
        );
        return new Response(JSON.stringify(response));
    },
} satisfies ExportedHandler<Env>;

This example imports the utils with import { runWithTools} from "@cloudflare/ai-utils" and follows the API reference below.

Moreover, in this example we define and describe a list of tools that the LLM can leverage to respond to the user query. Here, the list contains of only one tool, the sum function.

Abstracted by the runWithTools function, the following steps occur:

sequenceDiagram
    participant Worker as Worker
    participant WorkersAI as Workers AI

    Worker->>+WorkersAI: Send messages, function calling prompt, and available tools
    WorkersAI->>+Worker: Select tools and arguments for function calling
    Worker-->>-Worker: Execute function
    Worker-->>+WorkersAI: Send messages, function calling prompt and function result
    WorkersAI-->>-Worker: Send response incorporating function output

The ai-utils package is also open-sourced on Github ↗︎.

4. Local development & deployment

Follow steps 4 and 5 of the Workers AI Get Started Guide for local development and deployment.

Workers AI Embedded Function Calling charges

Embedded function calling runs Workers AI inference requests. Standard charges for inference (e.g. tokens) usage will be charged. Resources consumed (e.g. CPU time) during embedded functions' code execution will be charged just as any other Worker's code execution.

API reference

For more details, refer to API reference.

Was this helpful?

YesNo

On this page

Get Started (3) - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/workers-ai/features/function-calling/embedded/get-started/#page","headline":"Get Started","description":"Set up and deploy your first Workers AI project with embedded function calling.","url":"https://developers.cloudflare.com/workers-ai/features/function-calling/embedded/get-started/","inLanguage":"en","image":"https://developers.cloudflare.com/workers-ai/features/function-calling/embedded/get-started/og.png?v=8085b109897093fc","dateModified":"2026-04-21","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: Debug and resolve common issues with Workers AI embedded function calling. title: Troubleshooting image: https://developers.cloudflare.com/workers-ai/features/function-calling/embedded/troubleshooting/og.png?v=e7f024c00e0086e0


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/workers-ai/llms.txt
Use this file to discover all available pages before exploring further.