Writing custom nodes.
Declare a node's interface with comment directives, implement its run function, and use the supported runtime rather than backend internals.
A user node is Python with comment-based schema directives and a top-level
run() function. The directives declare the pins and presentation that Studio
discovers. They are not Python decorators.
Design the boundary around the computation
A node accepts declared inputs and controls, computes its result and returns declared outputs. Keep names, types and output shape consistent with the implementation. Validate the definition before execution and handle missing or invalid input explicitly. A visual preview and a saved schema are different evidence from a tested computation.
The same value, factor and result names are highlighted in declarations, function arguments and the return map. The example inputs 3 and 2 multiply to the result 6 at the node’s interface.
A small deterministic node
# @node title="Scale a Number" category="Math" icon="calculator" preview=true
# @task category="Analyze" subcategory="Data" filter="Math"
# @help Multiply a number by a factor.
# @style minHeight=260 widgetSpacing=60
# @input float value label="Value" default=1.0
# @input float factor label="Factor" default=2.0
# @output float result label="Result"
def run(value, factor):
return {"result": value * factor}
Open Code with Cmd+E (Ctrl+E on Windows/Linux), paste the source and use Add node to graph. Run it with a value of 3 and a factor of 2 to get 6. This example needs no provider key or additional package.
Preserve the contract
Use the declared pin names in the function and output dictionary. Every normal
return path must map output names to values. For an actionable failure, raise
an appropriate exception or return {"@error": "Explain what to correct"}. Here
@error is a dictionary key, not a comment directive. Do not fabricate a
successful result when required work failed.
Raw discovery needs a # @node title="..." directive. Saving assistant-generated
source also requires matching @node category and @task filter metadata.
Include help, style and correct pin declarations to make the node usable.
Test after changing the interface;
existing graphs can still refer to its earlier pin names.
Use the public runtime
User nodes have a restricted runtime. Built-in source can use privileges and
imports that user code does not have, so copying a built-in implementation may
need adaptation. Request helpers as parameters to run(): files, output,
log, progress, env, rest, web, connector, session, stream,
node, storage, vectordb, memory, fal, mcp, tools, runtime,
nodes, storyboard and timeline. package provides read-only bundled data
for directory-style node packages; it is unavailable to flat-file nodes and
plugins. Do not depend on backend
modules, direct filesystem access, or internal library paths.
Request helpers by their exact supported parameter names. An undeclared helper name is not injected automatically and can make the function fail to run.
Declare external packages with @require; hosted Kaitoi uses an administrator
allowlist. User source cannot import backend modules (app, backend, nodes,
runtime_sdk, sandbox_executor, sandbox_runtime) or direct system tools:
os, sys, pathlib, glob, fnmatch, shutil, zipfile, tarfile,
tempfile, subprocess, importlib, inspect, pkgutil, runpy, site,
builtins, ctypes, pickle, dill, cloudpickle, marshal, code,
boto3, botocore, aiobotocore. Calls to open, exec, eval, compile,
__import__, input, globals, locals and breakpoint, and private/dunder
reflection, are blocked. Use runtime helpers instead.
Give a node its own interface
A node can be Python alone, or Python plus its own interface written in HTML, CSS and JavaScript. The interface appears inside the node on the graph, and a Station can use it as its whole page (see Use a custom app). So a node can be anything from a small control to a full application: Motion Studio, a built-in node, pairs Python with an HTML, CSS and JavaScript editor for animated video.
Declare an output of type web, set preview=true, and return the page from
run():
# @node title="Status Badge" category="Web" icon="badge-check" preview=true
# @task filter="Web"
# @input string label label="Label" default="Ready"
# @output web view label="View"
HTML = """<div id="badge"></div>
<script>
document.getElementById('badge').textContent = (window.__STATE__ || {}).label || '';
</script>"""
def run(label, web, output):
page = web.from_html(HTML)
page.state = {"label": label}
return {"view": page.save(output, format="json")}
The page reads the values you set in state from window.__STATE__. For a
larger interface, use a node package with index.html, styles.css and
script.js in its assets folder, and load them with
web.from_files(directory=web.get_script_dir(), state={...}).
The page runs in a sandboxed frame, separate from the Studio page. It can load libraries from jsDelivr, unpkg or cdnjs, run web workers, and use WebAssembly, either embedded in the page or fetched over HTTPS, which suits work such as audio engines or simulations compiled from Rust or C++. Penny can write a node like this on every plan; editing its files by hand needs Studio or Enterprise, as for any node code.
Helper reference
Helpers are requested by their exact parameter names. Avoid helper names such as files, env and output for ordinary pins; collision behavior depends on the helper. Some helpers require account access, compatible input data or a trusted node; a parameter does not bypass those checks.
| Helpers | Purpose |
|---|---|
files |
Load managed files; write text, JSON or bytes |
output(".png") |
Allocate an output path; save actual image data to it |
log(message), progress(0.5) |
Send a log message or numeric progress |
env |
The running user's available Variables and provider keys; never log or return them |
rest |
HTTP requests and downloads through the supported client |
web, plot, gallery |
HTML previews, plots and library grids |
package |
Read-only bundled assets; directory-style nodes only, unavailable to flat nodes and plugins |
connector, mcp, tools |
Connected-account access, external MCP and tool calls under their permissions |
session, storage, memory, vectordb |
Session state, node storage, memory and vector operations |
stream, realtime |
Streaming previews and supported real-time operations |
imagetools, videotools, audio |
Image, video and audio utilities |
nodes, graph, trigger, node |
Catalog discovery, graph context and supported peer execution |
storyboard, timeline |
Editable media-document helpers |
fal, openrouter, bedrock |
Supported provider clients and uploads |
ollama, comfy, blender, runtime |
Connected local services; available on every plan, with the service running |
rerun, supersplat |
Supported 3D/multimodal visualization |
mentions, camera, mask |
Input/context-dependent values |
pretty |
Format diagnostic values; exclude secrets yourself |
Requesting pretty also supplies the pprint alias; requesting pprint
alone does not inject it. It is also available as kaitoi.pprint.
Lower-level rpc retains its server-side operation checks. lib reuses
modules and assets from the built-in node library. Prefer the focused helpers
above and review any code you reuse.
Test before publishing
Test valid, empty, and invalid inputs and any failure path. For media, inspect a real produced file; an empty path or placeholder file is not a usable output. Keep keys and private data out of source, diagnostics, and bundled assets.
Checks and next steps
Keep each metadata directive on its own line and preserve exact pin names
when editing an existing interface. @require declares a dependency; it does
not bypass the installation's package allowlist or guarantee that a package
can run in the selected environment.
The outer return dictionary maps output names to values. A file-backed
object output needs the managed JSON-file representation; returning an
arbitrary Python dictionary as that pin's value is not the same contract.
Use files.write_json(data) and return the managed file under the declared
output name, for example return {"details": files.write_json({"count": 3})}
for # @output object details.
Likewise, allocating an output path does not create valid media by itself.
Review generated-source diagnostics and implementation warnings, then test a small known example and failure cases. Static acceptance does not prove provider availability, media quality, complete implementation, or safe reuse with different inputs. See Node and data reference and Creating nodes with Penny.
Last reviewed against Kaitoi on 26 October 2026. View this page as Markdown