# Stof: run AI-generated logic safely, inside your app > Stof is JSON with functions: an open-source data runtime where one document holds data and the logic that works on it. Apps run it in a sandbox, in-process, in Rust, JavaScript/TypeScript, or Python, which makes it a safe way to run logic written by AI models, users, or customers. Stof is the policy engine behind Limitr. Stof is JSON with functions: one document holds data and the logic that works on it, and an app runs it in a sandbox, in-process, in Rust, JavaScript/TypeScript, or Python. Its main use is running logic that a model, user, or customer wrote, safely inside an app. Documents can only see themselves and the functions the host gives them; file system, environment, and network access are off by default. - Start with the overview: https://stof.dev/what-is-stof (what Stof is, use cases, comparisons with similar tools, FAQ) - Try it in the browser: https://stof.dev/playground - Install: npm i @formata/stof (JavaScript/TypeScript), cargo add stof (Rust), pip install stof (Python), cargo install stof-cli (command line) - Teach a model to write Stof: https://github.com/dev-formata-io/stof/blob/main/content/SKILL.md - Source (Apache 2.0): https://github.com/dev-formata-io/stof - In production: Stof is the policy engine behind Limitr (https://limitr.dev), a pricing runtime for usage-based and AI software made by Formata, Inc. ## Table of Contents - [What is Stof?](https://stof.dev/docs/start/what-is-stof.md): Stof is JSON with functions: an open-source data runtime for running AI-generated logic safely inside your app, in Rust, JavaScript/TypeScript, or ... - [About These Docs](https://stof.dev/docs/start/about.md): How these docs are organized, a suggested reading order, and how the runnable examples work. - [How Stof Works](https://stof.dev/docs/start/how-it-works.md): The ideas behind Stof: a document is a graph of objects, functions are data on those objects, documents are sandboxed, they can change themselves w... - [Install](https://stof.dev/docs/start/install.md): Install Stof for JavaScript/TypeScript, Python, Rust, or the command line, set it up in a browser or bundler, and add editor support. - [Stof Mission](https://stof.dev/docs/start/mission.md): Why Stof exists, the principles it is built around, where it came from, and where it is going. - [Quick Start](https://stof.dev/docs/start/quickstart.md): Run your first Stof document in the browser, then from your own app in a few minutes: data and logic in one document, called from TypeScript, and l... - [Tools](https://stof.dev/docs/start/tools.md): The stof command-line tool (run, test, leak checks, packages, docs), the VS Code extension and language server, and the skill that teaches AI model... - [Using Stof in Your App](https://stof.dev/docs/start/using-stof.md): How an app works with Stof documents: loading data and logic, calling functions, running by attribute, reading and writing values, giving documents... - [Async](https://stof.dev/docs/learn/async.md): Async in Stof: processes, async functions, await, running work in parallel with async calls and blocks, awaiting several promises at once, how proc... - [Attributes](https://stof.dev/docs/learn/attributes.md): Attributes in Stof: the #[name(value)] tags on fields, functions, and objects, the built-in ones, custom attributes as metadata, finding functions ... - [Collections](https://stof.dev/docs/learn/collections.md): Working with lists, maps, sets, and tuples: adding and removing, searching, sorting, set operations, and when braces make a map or set instead of a... - [Control Flow](https://stof.dev/docs/learn/control-flow.md): if and else, the ternary operator, switch (also as an expression), while and loop, for and for-in loops with index, first, and last, labeled break ... - [The Document Graph](https://stof.dev/docs/learn/document-graph.md): How a Stof document is structured: objects and the data attached to them, self, super, and root, how paths are resolved, named roots, creating obje... - [Error Handling](https://stof.dev/docs/learn/error-handling.md): Throwing and catching errors in Stof: throwing any value, structured errors as objects, catching by type, the error codes Stof's own errors use, pr... - [Documents and Fields](https://stof.dev/docs/learn/fields.md): Where to start learning Stof: a document is JSON with optional extras. Objects, fields, comments, typed fields, const, readonly and private fields,... - [Formats](https://stof.dev/docs/learn/formats.md): Every format Stof can parse and export (JSON, YAML, TOML, Stof, binary Stof, text, bytes, Markdown, URL-encoded, PDF, images, DOCX), where each is ... - [Functions](https://stof.dev/docs/learn/functions.md): Declaring and calling functions, return values, parameters (defaults, optional, named), what a function can see, arrow functions, functions as valu... - [Import & Export](https://stof.dev/docs/learn/import-and-export.md): Getting data and logic into and out of a document: stringify and blobify, parsing at runtime with parse(), the import statement, relative paths, fo... - [Memory Management](https://stof.dev/docs/learn/memory-management.md): Cleaning up objects in a Stof document: why objects created with new stay until dropped, using declarations and expressions for temporary objects, ... - [Null & Initialization](https://stof.dev/docs/learn/null-and-init.md): Working with null in Stof: missing fields are null, defaults with ??, null-safe calls with ?. and ?, math with null is an error, not-null types wit... - [Object Run](https://stof.dev/docs/learn/object-run.md): Running an object as a workflow with obj.run(): #[run] steps, ordering, steps from a type, overrides, nested steps, arguments, lists of steps, and ... - [Prototypes & Schemas](https://stof.dev/docs/learn/prototypes-and-schemas.md): Your own types in Stof: declaring them with #[type], creating instances with new, constructors, static functions, inheritance, qualified type names... - [Testing](https://stof.dev/docs/learn/testing.md): Tests inside Stof documents: #[test] functions, expected values, expected errors with #[errors], the assert functions, running tests with stof test... - [Types & Units](https://stof.dev/docs/learn/types-and-units.md): Stof's types: numbers, strings, booleans, versions, bytes, collections, objects and functions, units like 5km and 30s, casting with as, typeof and ... - [Units Reference](https://stof.dev/docs/learn/units-reference.md): Every unit Stof supports, by category. The abbreviation and the full name both work anywhere a unit is expected. - [Variables & References](https://stof.dev/docs/learn/variables-and-references.md): let and const, block scopes, destructuring objects, maps, lists, and tuples into variables, which values are copied and which are shared, copy(), a... - [Age Encryption (Age)](https://stof.dev/docs/libs/age-library.md): Public-key and passphrase encryption for parts of a document: generate identities, encrypt to a recipient, and decrypt with the matching key. - [File System Library (fs)](https://stof.dev/docs/libs/file-system-library.md): Reading and writing files from a Stof document. Off by default: hosts turn it on with allow_system(). Rust, Python, and the CLI. - [HTTP Network Library (Http)](https://stof.dev/docs/libs/http-library.md): Making HTTP requests from a Stof document and reading the responses. Off by default: the host turns it on, for any host or only an allowlist. - [Image Library (Image)](https://stof.dev/docs/libs/image-library.md): Loading, transforming, resizing, and exporting images. Available in Python, Rust (the "image" feature), and the CLI. - [Markdown Library (Md)](https://stof.dev/docs/libs/markdown-library.md): Converting Markdown strings to HTML or a structured JSON representation. - [Libraries](https://stof.dev/docs/libs/overview.md): The libraries built into Stof, where each one is available (JavaScript, Python, Rust, CLI), which ones the host has to turn on, and how to add your... - [PDF Library (Pdf)](https://stof.dev/docs/libs/pdf-library.md): Extracting text and images from PDF files. Available in Python, Rust (the "pdf" feature), and the CLI. - [Standard Library (Std)](https://stof.dev/docs/libs/standard-library.md): Every Std function: printing, assertions, parsing and exporting, collections, introspection, and process control. Std is implied, so Std::pln(...) ... - [Time Library (Time)](https://stof.dev/docs/libs/time-library.md): Timestamps, RFC-2822/3339 formatting, calendar arithmetic, and period boundaries — all working in plain millisecond units. - [Agent Tools at Runtime](https://stof.dev/docs/cookbook/agent-tools.md): Give an AI agent tools that are Stof functions: generate tool definitions for the model's API from the document, run the model's tool calls in a sa... - [Run Rules a Model Writes](https://stof.dev/docs/cookbook/ai-rules.md): Turn a plain-English policy into code with an AI model, check it, and run it on every request inside your app, safely: the contract, the prompt, th... - [A Self-Validating Config](https://stof.dev/docs/cookbook/config.md): App configuration in Stof: one type with every setting, its default, its units, and its validation rule; environments that only list what's differe... - [Rules Your Customers Write](https://stof.dev/docs/cookbook/customer-rules.md): Let each customer define their own business rules (here, invoice approvals) as Stof instead of building a rules engine or DSL: the vocabulary your ... - [Feature Flags and Entitlements](https://stof.dev/docs/cookbook/feature-flags.md): Feature flags and plan entitlements as a Stof document: targeting rules as functions, stable percentage rollouts, the same flags evaluated on the s... - [Cookbook](https://stof.dev/docs/cookbook/overview.md): Complete, real-world uses of Stof, with the app code and the Stof together: running rules a model writes, agent tools at runtime, customer-defined ... - [The Same Logic in the Browser and on the Server](https://stof.dev/docs/cookbook/shared-logic.md): Write business logic once, as a Stof document, and run it in the browser for instant feedback and on a Python or Node.js server for the real answer... - [Usage Limits and Pricing Plans](https://stof.dev/docs/cookbook/usage-pricing.md): Define pricing plans, usage limits, and overage billing as a Stof document, check limits in-process on every request, and change plans without a de... - [One Shape for Data from Many Sources](https://stof.dev/docs/cookbook/webhooks.md): Normalize webhooks and payloads from many providers and formats (JSON, form data, YAML) into one validated shape, with one small Stof mapping per s... - [Blob Library (Blob)](https://stof.dev/docs/libs/types/blob-library.md): Working with raw binary data — byte access, size, and Base64/UTF-8 encoding. - [Data Library (Data)](https://stof.dev/docs/libs/types/data-library.md): Low-level access to the opaque Data handles underneath every field and function. - [Function Library (Fn)](https://stof.dev/docs/libs/types/function-library.md): Calling, binding, and introspecting functions as first-class values. - [List Library (List)](https://stof.dev/docs/libs/types/list-library.md): Query, access, modify, remove, sort, and convert operations on the list type. - [Map Library (Map)](https://stof.dev/docs/libs/types/map-library.md): Query, access, and modify operations on the map type. Maps are ordered by key. - [Number Library (Num)](https://stof.dev/docs/libs/types/number-library.md): Rounding, trigonometry, logarithms, base representations, and unit inspection — automatically linked to int, float, and every unit type. - [Object Library (Obj)](https://stof.dev/docs/libs/types/object-library.md): Every Obj function — navigation, fields and data, prototypes, and structural operations on the obj type. - [Prompt Library (Prompt)](https://stof.dev/docs/libs/types/prompt-library.md): Building and reading a prompt value — a small tree of tagged text meant for AI-workflow contexts. - [Set Library (Set)](https://stof.dev/docs/libs/types/set-library.md): Query, modify, and set-algebra operations (union, intersection, difference) on the set type. - [String Library (Str)](https://stof.dev/docs/libs/types/string-library.md): Query, access, case/whitespace, modification, and regex operations on the str type. - [Tuple Library (Tup)](https://stof.dev/docs/libs/types/tuple-library.md): Indexed access into a fixed-length tuple. - [Semantic Version Library (Ver)](https://stof.dev/docs/libs/types/ver-library.md): Reading and modifying the major, minor, patch, release, and build portions of a ver value. - [We Deserve Better Than JSON as a DSL](https://stof.dev/blog/we-deserve-better-than-json-as-a-dsl.md): Every team ends up bolting a DSL onto JSON, YAML, or TOML. The formats aren't the problem. Asking data to carry logic it was never built to hold is. - [The Origin of Stof: From CAD Software to a Data Runtime](https://stof.dev/blog/origin-of-stof.md): Stof didn't start as a product. It came out of a decade of building parametric and geometry formats for CAD and graphics systems, and one idea abou... - [What Is a Data Runtime?](https://stof.dev/blog/what-is-a-data-runtime.md): A data runtime runs data directly, the way a JavaScript runtime runs JavaScript. Its documents carry their own logic, sandboxed and portable, inste... - [The Problem With JSON Schema (And What We Did Differently)](https://stof.dev/blog/the-problem-with-json-schema.md): JSON Schema describes your data's shape in a second file that has to be kept in sync by hand. Stof puts each validation rule on the field it protec... - [How to Safely Give AI Agents New Tools at Runtime](https://stof.dev/blog/safely-give-ai-agents-new-tools-at-runtime.md): Most systems handle "let an agent gain new tools" and "stop an agent from doing damage" with two separate systems, a tool registry and a permission... - [The Part of Your App You Can Hand to Something You Don't Trust](https://stof.dev/blog/safely-edit-app-state-with-ai-agents.md): AI agents already edit app state directly, and most formats have no defense when they do. Here's where Stof fits, and where it doesn't. - [The Heap Is Just a Document: How Stof Actually Works](https://stof.dev/blog/how-stof-actually-works.md): A Stof document holds data and the instructions for changing it, in the same place. Here's what that means, and why a Stof document behaves less li... - [Stof 0.10: Safe by Default, Errors You Can Read, and a Language Server](https://stof.dev/blog/stof-0-10.md): Stof 0.10 makes embedded documents sandboxed by default, with time limits and an HTTP allowlist, adds readable errors with locations, using declara...