What is Stof?
Stof is JSON that runs. It's JSON with functions: one document holds data and the logic that works on it, and your app runs the document in a sandbox, inside its own process, in Rust, JavaScript/TypeScript, or Python. The document can only use what your app hands it.
Stof is open source (Apache 2.0), made by Formata, Inc., and it's the engine behind Limitr.
- Website and docs: stof.dev
- Source: github.com/dev-formata-io/stof
- Packages:
@formata/stof(npm),stof(crates.io),stof(PyPI),stof-cli(command line) - Try it: the playground runs Stof in your browser
What it looks like
The data looks like JSON. The function next to it works on that data. Press Run:
price: 20
discount: (p: float): float => p * 0.8
fn total() -> float {
self.discount(self.price)
}Your app loads the document and works with it like data, logic included:
- TypeScript
- Python
- Rust
import { StofDoc } from '@formata/stof';
const doc = await StofDoc.new(source);
await doc.call('total'); // 16
doc.set('price', 50); // change the data
doc.parse('discount: (p: float): float => p * 0.5'); // change the logic, the same way
await doc.call('total'); // 25
from pystof import Doc
doc = Doc()
doc.parse(source)
doc.call('total') # 16.0
doc.set('price', 50) # change the data
doc.parse('discount: (p: float): float => p * 0.5') # change the logic, the same way
doc.call('total') # 25.0
use stof::model::Graph;
use stof::runtime::{Runtime, Val, Variable};
let mut graph = Graph::default();
graph.parse_stof_src(source, None, Default::default())?;
Runtime::call(&mut graph, "total", vec![])?; // 16.0
graph.set_field(Variable::val(Val::from(50i64)), "price", graph.main_root()); // change the data
graph.parse_stof_src("discount: (p: float): float => p * 0.5", None, Default::default())?; // change the logic, the same way
Runtime::call(&mut graph, "total", vec![])?; // 25.0
The discount is a function stored in a field, so new logic goes in the same way new data does: no build, no deploy.
What a document can hold
- Units. A number can carry its unit, and math between units converts:
5min + 30sis330s, and2GBconverts toMiBexactly. See Types & Units. - Types and rules. A type declares fields and functions once, and its
#[schema]rules check any object you point it at. See Prototypes & Schemas. - Tests.
#[test]functions live next to what they check, and run withstof testor from your app. See Testing. - Everything else you'd expect: collections, destructuring, async, attributes, and imports and exports for JSON, YAML, TOML, and compact binary.
The sandbox
A document sees itself and the functions your app registers, nothing else:
- TypeScript
- Python
- Rust
// now Host::greet exists in the document
doc.lib('Host', 'greet', (name) => `hello, ${name}`);
// and network access, to this host only
doc.allowHttp(['api.example.com']);
# now Host::greet exists in the document
doc.lib('Host', 'greet', lambda name: f'hello, {name}')
# and network access, to this host only
doc.allow_http_hosts(['api.example.com'])
// your functions are LibFuncs: graph.insert_libfunc(..) (see Using Stof in Your App)
// network access, to this host only (the http feature)
graph.allow_http_hosts(["api.example.com"]);
The file system, network, and environment are off by default in every language, and a call to anything your app didn't provide fails with an error that says so. Every call has a time limit and a memory limit. Parts you trust can be sealed so nothing changes them, and code from somewhere else can be confined to what you list. See Security and the Sandbox.
What it's for
- Logic someone else writes. A model, a user, or a customer gives you rules, filters, transforms, scoring, or agent tools. Check them, test them, and run them in-process on every request.
- Behavior per customer. One document per customer holds their rules, limits, and state. An agent can edit it, a person reviews the diff, and your app behaves differently for that customer with no deploy.
- Config that checks itself. Validate and compute values in the config, and still export plain JSON, YAML, or TOML.
- The rules engine, config format, or DSL you were about to build. Describe your domain in Stof instead.
- Logic that has to run everywhere. Send a document, functions included, and get the same answer in the browser, on a server, in a worker, or at the edge.
Runnable versions in the playground: rules a model writes, agent tools, customer rules, usage pricing, a self-validating config, webhooks into one shape, and checkout totals.
Use something else for your own application code, compute-heavy work, or long-running services. Write those in your main language and give them to Stof documents as functions.
How it works: everything is data
Stof has functions, but it isn't a language for writing apps. A Stof document is a graph of objects, and fields, functions, types, and docs are all pieces of data attached to those objects, like components in an entity-component system:
root
├── price: 20 field
├── discount: (p) => p * 0.8 field holding a function
└── total() function, data on the object like a field
So anything you can do with data, you can do with logic: merge two documents, pull one object out with its functions, diff two versions, store it in a database, or send it over the wire. A document can also change while it runs, parsing new fields and functions into itself or dropping them.
Key facts
| Fact | Detail |
|---|---|
| What it is | A data runtime: JSON with functions, types, units, and schemas, run in a sandbox |
| Runs in | Rust (native), JavaScript/TypeScript (WebAssembly, in Node.js and browsers), Python |
| Sandbox | Documents can only see themselves and the functions the host gives them. File system, environment, file imports, and network are off by default. Each call has a time and memory limit. |
| Speed | A small document parses and runs in under a millisecond; calls after that take microseconds |
| Formats | Imports and exports JSON, YAML, TOML, and more; compact binary (bstf) for sending documents, functions included |
| Language features | Types, prototypes, schemas (#[schema]), units (5min + 30s == 330s), destructuring, async, attributes, tests in the document (#[test]) |
| Tooling | stof CLI (stof run, stof test, stof lsp), a VS Code extension, and a skill file to teach AI models Stof |
| License | Apache 2.0 |
| Made by | Formata, Inc. Created by CJ Cummings. |
Stof in production: Limitr
Stof is the engine behind Limitr, a pricing runtime for usage-based and AI software. Every customer's plan, limits, and credits are a Stof document, pushed to running apps and evaluated in-process on every call.
How Stof compares
Stof sits between several kinds of tools. Here's where it overlaps and where it differs.
Code sandboxes for AI (containers, microVMs like Firecracker, hosted sandboxes like E2B or Modal, Pyodide, V8 isolates): these run general Python or JavaScript in isolation, out of process. Choose them when the model needs a full language and its packages. Choose Stof when the logic is small, should run inside your app with no infrastructure, and should only reach what you allow.
Function calling and MCP: a model calls tools you already wrote. Stof works alongside them: the model can write new logic as a document, and that logic calls the same functions you expose.
Rules engines and JSON rule formats (JSON Logic, json-rules-engine, Drools, GoRules): rules as data or a separate rules language. Stof rules are readable code that is also data, with types, functions, and schemas, in one document that can carry its own data too.
Expression and policy languages (Google's CEL, Open Policy Agent and Rego, AWS Cedar): designed to evaluate expressions or authorization policies against input. Stof is broader: whole documents with data, functions, types, and state that can change while they run.
Configuration languages (Jsonnet, CUE, Dhall, Pkl, Nickel, KCL): these mostly generate static JSON or YAML. A Stof document can export static JSON, YAML, or TOML too, but it also stays live: its functions run when your app calls them.
Embedded scripting languages (Lua, Starlark, Rhai, QuickJS): these embed a language for your code to call. In Stof the logic lives inside the data document, so it moves, merges, and serializes with the data.
WebAssembly (Wasmtime, Extism): portable compiled code. Stof is readable source that models can write directly and that is small to send, with no compile step. (The Stof runtime itself is compiled to WebAssembly for JavaScript.)
Schema and validation tools (JSON Schema, Zod, Pydantic): Stof documents can validate themselves with #[schema], and the schema is part of the same document as the data.
FAQ
Is Stof a programming language? Stof has a language for its functions, but it's a data runtime: a document format where data and logic live together, plus the library that runs it. You don't write apps in Stof. You give your app documents that it loads, runs, and exports.
Is it safe to run Stof that an AI model wrote? Yes, that's what it's built for. A document can only read and change itself and call the functions your app gives it. File system, environment variables, file imports, and network access are off by default in every language, and a host turns them on explicitly (with an allowlist for network hosts). Each call has a time and memory limit.
Can AI models write Stof? Yes. It reads like JSON and JavaScript, which models already know. For better results, give your agent the Stof skill. Errors give the exact line and a plain explanation, so a model can fix its own mistakes.
Is JSON valid Stof? Yes. Any JSON document is valid Stof, so you can add types and functions to data you already have and export it back to JSON, YAML, or TOML.
Which languages can run Stof?
Rust (the stof crate), JavaScript and TypeScript through WebAssembly (the @formata/stof npm package, for Node.js and browsers), and Python (the stof package on PyPI). The same document gives the same result in each.
How fast is it? It runs inside your process, with no server or cold start. A small document parses and runs in under a millisecond, and later calls take microseconds. See Performance for numbers and tips.
Who makes Stof? Formata, Inc., the company behind Limitr. Stof was created by CJ Cummings.
Where do I start? Try the playground, then the quick start. Questions are welcome on Discord.