Skip to main content

Stof 0.14: AI in Stof

CJ Cummings
Creator and maintainer of Stof, co-founder & CEO of Limitr

Stof 0.14 puts AI inside the document. A doc can call models, hand them its own functions as tools, run agents, and use remote MCP servers, and every model call and tool call goes through hooks the doc (or your app) controls. And the big one: a doc can declare a function by what it should do, and have a model write it. That's intents.

npm i @formata/stof@latest # TypeScript / JavaScript
pip install -U stof # Python
cargo add stof --features full # Rust (or the "ai" feature)

An agent in a document​

import '@formata/ai';

Ai ai: {} // anthropic is a provider it knows: give it a key, and that's the setup

desk: {
#[tool({'description': 'Look up an order', 'params': {'id': 'the order number'}})]
fn order(id: int) -> map { {'id': id, 'status': 'refunded on Nov 28'} }
}

#[main]
fn main() {
using request = new {
model: 'anthropic/claude-sonnet-5-5',
messages: [new { role: 'user', content: 'Where is my refund for order 1042?' }],
};
const run = await self.ai.agent(request, self.desk);
pln(run.text);
}

A tool is a function with #[tool]: its signature is the schema. The model can call only the tools a call offered, a tool that throws is an error result the model can recover from, and an agent stops on its limits (turns, tool calls, time) with everything so far, so it can go on. Try it in the playground, with a mock model standing in for the key.

Intents​

#[intents]
Ai writer: { intent_model: 'anthropic/claude-sonnet-5-5' }

#[examples([([10], 100), ([50], 400)])]
intent fn price(seats: int) -> int 'The monthly price: $10 a seat, 20% off at 50 or more.';

Say what a function should do, and skip writing it. The first time price is called, a model writes it in Stof, with the signature you declared. It's sealed in a sandbox (it reads and calls only what the intent gives it, with no network, files, or environment unless you say so), and it's only kept once your examples and checks pass. Every call after that runs what it wrote: no model, no tokens, the same answer every time, and code in the doc you can read. Change the intent and it's written again.

You can write them all ahead of time (build_intents(), in CI), ship the doc with the code in it, and run with no model at all. Checks can be intents too, so a model can write the checks for what another model writes. For what can't be code, #[each] has a model do it on every call. And Stof only looks for an executor, so anything with an intent function can be one: a test double, or code served from somewhere else.

We think this is where programming with Stof goes: say what you want, say how you'll know it's right, and let the runtime keep the code honest. Try it, read the intents guide, or the post on intents.

One shape for every provider​

openai_chat speaks Chat Completions (OpenAI, and most compatible providers and local servers), openai speaks the Responses API (reasoning goes back between turns, encrypted), and anthropic speaks the Messages API (thinking with signatures, prompt caching, token counting). Requests, responses, stream events, usage, and errors have one shape whatever the provider, so a fallback from one to another is a list on the request:

fallback: ['openai/gpt-5.6-terra', 'local/llama-4']

For anything else (a vendor SDK, a company proxy, signed requests), the app makes the call and hands back events: doc.aiProvider(name, generator) in TypeScript, doc.ai_provider in Python.

Hooks around every call​

This is the part we built it for. #[ai-before], #[ai-after], and #[ai-error] run around every model call, and #[ai-approve] and #[ai-result] around every tool call. A before hook can cap max_tokens, take tools off the offer, route to another model, or refuse; every call that ran its before hooks ends with the after or the error hooks, so a budget held before a call is always settled or released. Usage keeps cached input apart, so each kind can have its own price.

That's how Limitr connects: a pricing policy is a Stof document, and now it sits in front of every model call and tool call an agent makes. The meter example shows the shape.

Keys a document can't see​

A provider's key is a name. The app gives the key with secret(name, key, hosts), and Stof puts it in the request's header only for the hosts it was given for. Document code sees a placeholder: printing it, storing it, or sending it anywhere else gives the placeholder. That matters when the document came from somewhere else, or from a model. A doc can give its own keys too (Http::secret('anthropic', env('ANTHROPIC_API_KEY'), ['api.anthropic.com']) when the CLI runs it), and the app's key with the same name wins.

MCP servers​

McpServer is a client for remote MCP servers over Streamable HTTP. Its tools go in a request like #[tool] functions, through the same hooks, and its resources and prompts are there to read. It speaks the stateless 2026-07-28 revision and falls back to older servers on its own, and the TypeScript and Python SDKs do the OAuth login (doc.mcpLogin).

Also in 0.14​

  • Built-in packages: import '@formata/ai' works without a file in the CLI, Rust, and Python; TypeScript loads it with await doc.ai(), as a module of its own, so bundlers split it off.
  • Streaming HTTP: Http::stream and Http::next read a body as it arrives (bytes or server-sent events), and a call a host stops closes its streams.
  • Caught errors stay quiet, thrown values read as data, and an awaited error says where it started.
  • ++, --, ??=, and destructuring assignment.

The AI guides cover it task by task, and the CHANGELOG has everything.