Skip to main content
Early Beta — The Web SDK is in early beta. APIs may change between releases.

Overview

The Web SDK supports Tool Calling (function calling) and Structured Output (JSON schema-guided generation). These features enable LLMs to interact with external tools and produce structured, type-safe responses — all running on-device via WebAssembly.

Package Imports

Tool Calling and Structured Output are exported from @runanywhere/web-llamacpp (the LLM backend), not from @runanywhere/web:
Common mistake: importing ToolCalling or StructuredOutput from @runanywhere/web. These are NOT exported from the core package. Always import from @runanywhere/web-llamacpp.

Tool Calling

How It Works

The orchestration loop is: generate → parse tool call → execute → feed result back → repeat until done.

Basic Usage

For best tool calling results, use the LFM2 1.2B Tool model which is specifically optimized for function calling:
The smaller LFM2 350M also works but may produce less reliable tool call formatting.

API Reference

ToolCalling (Singleton)

Types

ToolExecutor

The executor function receives parsed arguments as Record<string, ToolValue> and must return Record<string, ToolValue>:
TypeScript gotcha: If your executor has try/catch branches that return different keys, TypeScript may infer a union type where some properties are undefined, which is not assignable to ToolValue. Fix this by explicitly annotating the return type:

ToolValue & Helpers

ToolValue is a tagged union that wraps JavaScript values for type-safe serialization:
Helper functions for working with ToolValue:

ToolCallFormat

The format is auto-detected based on the loaded model, or you can override it:

Working Examples

Weather Tool (Fake Data)

A simple tool that returns randomized weather data — useful for demos:

Weather Tool (Real API)

A tool that calls the Open-Meteo API for real weather data:

Calculator Tool

Evaluates mathematical expressions using JavaScript:

Time / Timezone Tool

Returns the current time for any IANA timezone:

Random Number Tool

Generates a random integer in a range — demonstrates getNumberArg:

Manual Execution Mode

When autoExecute: false, the SDK returns tool calls without executing them. You handle execution and feed results back:

Registering Custom Tools at Runtime

You can dynamically register tools — useful for plugin systems or user-configurable tools:

Complete React Component

A full React component with tool registration, execution trace, and UI:
ToolCallingDemo.tsx

Lifecycle Management

Always clean up tools on component unmount in React apps. If multiple components register tools with the same name, they will overwrite each other. Use ToolCalling.clearTools() in useEffect cleanup functions.

Structured Output

Generate type-safe JSON responses from LLMs using JSON schema validation.

Basic Usage

API Reference

Workflow

1

Define schema

Create a JSON Schema that describes the desired output format.
2

Prepare prompt

Use getSystemPrompt() or preparePrompt() to build schema-aware prompts.
3

Generate

Run TextGeneration.generate() with the prepared prompt.
4

Validate

Use validate() to extract and verify the JSON output.
All JSON extraction and schema validation runs in the C++ WASM layer for performance. The TypeScript API is a thin wrapper around the rac_structured_output_* C functions.

LLM Generation

Text generation

VLM

Vision Language Models

System Prompts

Control AI behavior

Best Practices

Performance optimization