Run your first actor in five minutes
Install one package and run one program. An order actor fills its cart, charges the card and retries on its own when the payment service is down.
npm install @tinyactors/node@tinyactors/node 0.4.0, 19 November 2026What changedNode.js and Bun guide
pip install tinyactorstinyactors 0.4.0, 19 November 2026What changedPython guide
npm install @tinyactors/wasm@tinyactors/wasm 0.4.0, 19 November 2026What changedBrowsers guide
curl -L https://github.com/tinyactors-dev/tinyactors-core/releases/download/v0.5.0/tinyactors-0.5.0-src.tar.gz | tar xztinyactors-core 0.5.0, 26 November 2026What changedC guide
Write a chart and run it
The packages need Node.js 22.3 or Bun, Python 3.13 or newer, or a C compiler. Save the chart as order.scxml and the program beside it, then run the program; the tabs follow the package you picked above.
import { readFileSync } from 'node:fs';
import { compileCharts, Host } from '@tinyactors/node';
let charges = 0;
const payments = {
receive(send) { // the chart's <send type="payments">
console.log('charge', JSON.stringify(send.data));
if (++charges === 1) send.fail(); // down for the first charge: the order gets error.communication
else send.answer('charged'); // an event back to the order
},
};
const compiled = compileCharts([{ name: 'order.scxml', text: readFileSync('order.scxml', 'utf8') }]);
const host = new Host(compiled, {
system: 'shop',
ioProcessors: { payments },
log: (entry) => console.log('log', entry.text),
});
compiled.close();
const order = host.spawn('order');
order.send('add', { item: 'notebook', price: 30 });
order.send('add', { item: 'pen', price: 12 });
order.send('checkout');
console.log('paid', await order.done); // the order retried on its own
host.close();<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0"
datamodel="ecmascript" name="order" initial="cart">
<datamodel>
<data id="lines" expr="[]"/>
<data id="total" expr="0"/>
<data id="attempts" expr="0"/>
<data id="problem" expr="null"/>
</datamodel>
<!-- The customer fills the cart: each add is a line with its item and price.
Checkout needs at least one line. -->
<state id="cart">
<transition event="add">
<assign location="lines" expr="lines.concat([{item: _event.data.item, price: _event.data.price}])"/>
<assign location="total" expr="total + _event.data.price"/>
</transition>
<transition event="checkout" cond="lines.length > 0" target="paying">
<assign location="attempts" expr="0"/>
<assign location="problem" expr="null"/>
</transition>
</state>
<!-- The order asks the payment service to charge the total, and waits.
A declined card sends it back to the cart; a service that cannot be
reached is tried again, three times in all. -->
<state id="paying">
<onentry>
<assign location="attempts" expr="attempts + 1"/>
<send type="payments" event="charge">
<param name="amount" expr="total"/>
<param name="items" expr="lines.map((line) => line.item)"/>
</send>
</onentry>
<transition event="charged" target="paid"/>
<transition event="declined" target="cart">
<assign location="problem" expr="_event.data.reason"/>
</transition>
<transition event="error.communication" cond="attempts < 3" target="retrying"/>
<transition event="error.communication" target="cart">
<assign location="problem" expr="'the payment service could not be reached'"/>
</transition>
</state>
<!-- The payment service could not be reached: the order waits a second,
then charges again, on its own. -->
<state id="retrying">
<onentry>
<log label="order" expr="'payment service unreachable, attempt ' + attempts + ': retrying in 1 s'"/>
<send event="retry" delay="1s"/>
</onentry>
<transition event="retry" target="paying"/>
</state>
<!-- The order is paid: its total is what it hands back as it ends. -->
<final id="paid">
<donedata><content expr="total"/></donedata>
</final>
</scxml>import { compileCharts, Host } from '@tinyactors/wasm';
let charges = 0;
const payments = {
receive(send) { // the chart's <send type="payments">
console.log('charge', JSON.stringify(send.data));
if (++charges === 1) send.fail(); // down for the first charge: the order gets error.communication
else send.answer('charged'); // an event back to the order
},
};
const chart = await fetch(new URL('order.scxml', import.meta.url)).then((response) => response.text());
const compiled = compileCharts([{ name: 'order.scxml', text: chart }]);
const host = new Host(compiled, {
system: 'shop',
ioProcessors: { payments },
log: (entry) => console.log('log', entry.text),
});
compiled.close();
const order = host.spawn('order');
order.send('add', { item: 'notebook', price: 30 });
order.send('add', { item: 'pen', price: 12 });
order.send('checkout');
console.log('paid', await order.done); // the order retried on its own
host.close();<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0"
datamodel="ecmascript" name="order" initial="cart">
<datamodel>
<data id="lines" expr="[]"/>
<data id="total" expr="0"/>
<data id="attempts" expr="0"/>
<data id="problem" expr="null"/>
</datamodel>
<!-- The customer fills the cart: each add is a line with its item and price.
Checkout needs at least one line. -->
<state id="cart">
<transition event="add">
<assign location="lines" expr="lines.concat([{item: _event.data.item, price: _event.data.price}])"/>
<assign location="total" expr="total + _event.data.price"/>
</transition>
<transition event="checkout" cond="lines.length > 0" target="paying">
<assign location="attempts" expr="0"/>
<assign location="problem" expr="null"/>
</transition>
</state>
<!-- The order asks the payment service to charge the total, and waits.
A declined card sends it back to the cart; a service that cannot be
reached is tried again, three times in all. -->
<state id="paying">
<onentry>
<assign location="attempts" expr="attempts + 1"/>
<send type="payments" event="charge">
<param name="amount" expr="total"/>
<param name="items" expr="lines.map((line) => line.item)"/>
</send>
</onentry>
<transition event="charged" target="paid"/>
<transition event="declined" target="cart">
<assign location="problem" expr="_event.data.reason"/>
</transition>
<transition event="error.communication" cond="attempts < 3" target="retrying"/>
<transition event="error.communication" target="cart">
<assign location="problem" expr="'the payment service could not be reached'"/>
</transition>
</state>
<!-- The payment service could not be reached: the order waits a second,
then charges again, on its own. -->
<state id="retrying">
<onentry>
<log label="order" expr="'payment service unreachable, attempt ' + attempts + ': retrying in 1 s'"/>
<send event="retry" delay="1s"/>
</onentry>
<transition event="retry" target="paying"/>
</state>
<!-- The order is paid: its total is what it hands back as it ends. -->
<final id="paid">
<donedata><content expr="total"/></donedata>
</final>
</scxml>import asyncio
import json
import tinyactors
class Payments:
charges = 0
def receive(self, send): # the chart's <send type="payments">
print("charge", json.dumps(send.data, separators=(",", ":")))
self.charges += 1
if self.charges == 1:
send.fail() # down for the first charge: the order gets error.communication
else:
send.answer("charged") # an event back to the order
async def main():
with open("order.scxml") as file:
chart = file.read()
with tinyactors.compile_charts([("order.scxml", chart)]) as compiled:
host = tinyactors.Host(compiled, system="shop",
io_processors={"payments": Payments()},
log=lambda entry: print("log", entry.text))
async with host:
order = host.spawn("order")
order.send("add", {"item": "notebook", "price": 30})
order.send("add", {"item": "pen", "price": 12})
order.send("checkout")
print("paid", await order.done) # the order retried on its own
asyncio.run(main())<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0"
datamodel="ecmascript" name="order" initial="cart">
<datamodel>
<data id="lines" expr="[]"/>
<data id="total" expr="0"/>
<data id="attempts" expr="0"/>
<data id="problem" expr="null"/>
</datamodel>
<!-- The customer fills the cart: each add is a line with its item and price.
Checkout needs at least one line. -->
<state id="cart">
<transition event="add">
<assign location="lines" expr="lines.concat([{item: _event.data.item, price: _event.data.price}])"/>
<assign location="total" expr="total + _event.data.price"/>
</transition>
<transition event="checkout" cond="lines.length > 0" target="paying">
<assign location="attempts" expr="0"/>
<assign location="problem" expr="null"/>
</transition>
</state>
<!-- The order asks the payment service to charge the total, and waits.
A declined card sends it back to the cart; a service that cannot be
reached is tried again, three times in all. -->
<state id="paying">
<onentry>
<assign location="attempts" expr="attempts + 1"/>
<send type="payments" event="charge">
<param name="amount" expr="total"/>
<param name="items" expr="lines.map((line) => line.item)"/>
</send>
</onentry>
<transition event="charged" target="paid"/>
<transition event="declined" target="cart">
<assign location="problem" expr="_event.data.reason"/>
</transition>
<transition event="error.communication" cond="attempts < 3" target="retrying"/>
<transition event="error.communication" target="cart">
<assign location="problem" expr="'the payment service could not be reached'"/>
</transition>
</state>
<!-- The payment service could not be reached: the order waits a second,
then charges again, on its own. -->
<state id="retrying">
<onentry>
<log label="order" expr="'payment service unreachable, attempt ' + attempts + ': retrying in 1 s'"/>
<send event="retry" delay="1s"/>
</onentry>
<transition event="retry" target="paying"/>
</state>
<!-- The order is paid: its total is what it hands back as it ends. -->
<final id="paid">
<donedata><content expr="total"/></donedata>
</final>
</scxml>/* cc main.c host.c -ltinyactors_datamodel_ecmascript -ltinyactors_runtime -ltinyactors -lm */
#include <stdio.h>
#include "host.h"
typedef struct payments {
int charges;
} payments;
/* What the order sends out, and how this program answers. */
static void handle(host *h, uint32_t effect) {
payments *service = h->application;
switch (ta_host_effect_number(h->host, effect, TA_EFFECT_FIELD_KIND)) {
case TA_EFFECT_SEND: /* the chart's <send type="payments"> */
host_print(h, "charge", effect);
if (++service->charges == 1) host_fail(h, effect); /* down: the order gets error.communication */
else host_answer(h, effect, "charged"); /* an event back to the order */
break;
case TA_EFFECT_LOG: { /* the chart's <log> */
ta_string_view text = ta_host_effect_text(h->host, effect, TA_EFFECT_FIELD_EVENT);
printf("log %.*s\n", (int)text.length, text.pointer);
break;
}
case TA_EFFECT_DONE: /* the order reached <final id="paid">: its total */
host_print(h, "paid", effect);
break;
default:
break;
}
}
int main(void) {
payments service = {0};
host h = {.application = &service};
if (host_open(&h, &(host_options){"order.scxml", "shop", "payments"}) != TA_OK) return 1;
uint64_t order = host_spawn(&h, "order");
host_send(&h, (host_event){order, "add", "({item: 'notebook', price: 30})"});
host_send(&h, (host_event){order, "add", "({item: 'pen', price: 12})"});
host_send(&h, (host_event){order, "checkout", NULL});
host_run(&h, handle); /* until the order is paid: it retries on its own */
host_close(&h);
return 0;
}/* A host for one chart, its loop and its payloads: what main.c uses. */
#ifndef HOST_H
#define HOST_H
#include <tinyactors.h>
#include <tinyactors_datamodel_ecmascript.h>
#include <tinyactors_runtime.h>
typedef struct host {
ta_host *host;
ta_datamodel_ecmascript *ecmascript;
ta_datamodel_binding datamodel;
void *application; /* the program's own state, for its handler */
} host;
typedef struct host_options {
const char *chart; /* the chart's file */
const char *system; /* the system id */
const char *io_processor; /* the one I/O processor type the chart sends to */
} host_options;
/* An event: its data an ECMAScript expression, or NULL for none. */
typedef struct host_event {
uint64_t session;
const char *name;
const char *data;
} host_event;
/* Called for each effect of a batch, before the batch is acknowledged. */
typedef void host_handler(host *h, uint32_t effect);
ta_status host_open(host *h, const host_options *options);
uint64_t host_spawn(host *h, const char *chart);
ta_status host_send(host *h, host_event event);
/* Runs until nothing is left to do, timers included, handing each effect to `handle`. */
void host_run(host *h, host_handler *handle);
/* Answers the send `effect` with the event `name`, as its I/O processor. */
ta_status host_answer(host *h, uint32_t effect, const char *name);
/* Fails the send `effect`: its sender gets error.communication. */
ta_status host_fail(host *h, uint32_t effect);
/* Prints `label` and the effect's payload as JSON. */
void host_print(host *h, const char *label, uint32_t effect);
void host_close(host *h);
#endif#define _POSIX_C_SOURCE 200809L /* nanosleep */
#include "host.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* The config, as CBOR: {"systemID": system, "ioProcessors": [{"type": io_processor}]} */
static ta_status open_host(host *h, const host_options *options, ta_allocator *allocator) {
unsigned char room[128];
ta_cbor_writer writer;
ta_cbor_writer_init(&writer, (ta_buffer){room, sizeof room, 0}, allocator);
ta_cbor_write_map(&writer, 2);
ta_cbor_write_text(&writer, ta_string("systemID"));
ta_cbor_write_text(&writer, ta_string(options->system));
ta_cbor_write_text(&writer, ta_string("ioProcessors"));
ta_cbor_write_array(&writer, 1);
ta_cbor_write_map(&writer, 1);
ta_cbor_write_text(&writer, ta_string("type"));
ta_cbor_write_text(&writer, ta_string(options->io_processor));
ta_bytes config;
ta_status status = ta_cbor_writer_bytes(&writer, &config);
ta_host_environment environment = {.size = sizeof environment, .datamodel_count = 1,
.datamodels = &h->datamodel, .allocator = *allocator};
if (status == TA_OK) status = ta_host_create(config, &environment, &h->host);
ta_cbor_writer_free(&writer); /* read during the call */
return status;
}
/* The chart's file, compiled into an image the host copies. */
static ta_status add_chart(host *h, const char *path, ta_allocator allocator) {
ta_bytes text;
ta_status status = ta_runtime_load_file(path, &text);
if (status != TA_OK) return status;
ta_compile_options options = {.size = sizeof options, .alloc = allocator};
ta_source source = {ta_string(path), {(const char *)text.pointer, (uint32_t)text.length}};
ta_compile_result *compiled = NULL;
status = ta_compile_charts(&options, (ta_source_list){&source, 1}, &compiled);
if (status == TA_OK) {
ta_string_view image = ta_compile_image(compiled);
uint32_t segment = 0;
status = ta_host_add_image(h->host, (ta_bytes){(const unsigned char *)image.pointer, image.length}, &segment);
}
ta_compile_free(compiled);
ta_runtime_free_file(text);
return status;
}
ta_status host_open(host *h, const host_options *options) {
ta_allocator allocator = ta_runtime_malloc();
h->ecmascript = ta_datamodel_ecmascript_new(NULL);
h->datamodel = ta_datamodel_ecmascript_binding(h->ecmascript);
ta_status status = open_host(h, options, &allocator);
return status == TA_OK ? add_chart(h, options->chart, allocator) : status;
}
uint64_t host_spawn(host *h, const char *chart) {
ta_host_chart_ref ref;
if (ta_host_chart(h->host, ta_string(chart), &ref) != TA_OK) return 0;
ta_spawn spawn = {.segment = ref.segment, .chart = ref.chart};
uint64_t session = 0;
return ta_host_spawn(h->host, &spawn, &session) == TA_OK ? session : 0;
}
ta_status host_send(host *h, host_event e) {
ta_event_in event = {.size = sizeof event, .session = e.session, .name = ta_string(e.name)};
if (e.data) event.data = ta_datamodel_ecmascript_payload_eval(h->ecmascript, 0, ta_string(e.data));
ta_status status = ta_host_send(h->host, &event);
if (status != TA_OK && event.data) /* a refused event's data is still ours */
h->datamodel.vtable->release(h->datamodel.datamodel, event.data);
return status;
}
/* Every effect handed out, each batch acknowledged after its handler saw it. */
static void drain(host *h, host_handler *handle) {
const ta_host_effect *effects;
uint32_t count;
while ((count = ta_host_effects(h->host, &effects)) > 0) {
for (uint32_t i = 0; i < count; i++) handle(h, i);
ta_host_effects_ack(h->host); /* what the batch handed out ends here */
}
}
/* Sleeps until the host's clock, in nanoseconds (ta_runtime_now_ns), reads `deadline`. */
static void sleep_until(ta_time deadline) {
ta_time now = ta_runtime_now_ns();
if (deadline <= now) return;
ta_time wait = deadline - now;
struct timespec duration = {(time_t)(wait / 1000000000u), (long)(wait % 1000000000u)};
nanosleep(&duration, NULL);
}
void host_run(host *h, host_handler *handle) {
for (;;) {
drain(h, handle);
ta_host_advance_time(h->host, (ta_system_time){ta_runtime_now_ns(), 0}); /* timers fire as it passes */
ta_run run = ta_host_run(h->host, 10000);
drain(h, handle);
if (run.status == TA_RUN_FAULT) return;
if (run.status != TA_RUN_IDLE) continue; /* more to run now */
if (run.next_deadline == UINT64_MAX) return; /* nothing left, not even a timer */
sleep_until(run.next_deadline); /* a program with an event loop arms a timer instead */
}
}
ta_status host_answer(host *h, uint32_t effect, const char *name) {
ta_event_in event = {.size = sizeof event, .name = ta_string(name)};
event.session = ta_host_effect_number(h->host, effect, TA_EFFECT_FIELD_SESSION);
event.sendid = ta_host_effect_text(h->host, effect, TA_EFFECT_FIELD_SENDID);
return ta_host_send(h->host, &event);
}
ta_status host_fail(host *h, uint32_t effect) {
ta_event_in event = {.size = sizeof event, .flags = TA_EVENT_IN_ERROR_COMMUNICATION};
event.session = ta_host_effect_number(h->host, effect, TA_EFFECT_FIELD_SESSION);
event.sendid = ta_host_effect_text(h->host, effect, TA_EFFECT_FIELD_SENDID);
return ta_host_send(h->host, &event);
}
void host_print(host *h, const char *label, uint32_t effect) {
ta_payload payload = ta_host_effect_number(h->host, effect, TA_EFFECT_FIELD_PAYLOAD);
ta_bytes cbor;
if (ta_datamodel_ecmascript_payload_to_cbor(h->ecmascript, payload, &cbor) != TA_OK) return;
char room[256];
ta_buffer json = {room, sizeof room, 0};
ta_status status = ta_cbor_diagnostic(cbor, &json); /* CBOR as text: JSON for these values */
if (status == TA_ERROR_NO_MEMORY) { /* json.length is the room it needs */
json = (ta_buffer){malloc(json.length), json.length, 0};
status = json.pointer ? ta_cbor_diagnostic(cbor, &json) : TA_ERROR_NO_MEMORY;
}
if (status == TA_OK) printf("%s %.*s\n", label, (int)json.length, (const char *)json.pointer);
if (json.pointer != room) free(json.pointer);
}
void host_close(host *h) {
ta_host_free(h->host);
ta_datamodel_ecmascript_free(h->ecmascript); /* after every host that uses it */
}<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0"
datamodel="ecmascript" name="order" initial="cart">
<datamodel>
<data id="lines" expr="[]"/>
<data id="total" expr="0"/>
<data id="attempts" expr="0"/>
<data id="problem" expr="null"/>
</datamodel>
<!-- The customer fills the cart: each add is a line with its item and price.
Checkout needs at least one line. -->
<state id="cart">
<transition event="add">
<assign location="lines" expr="lines.concat([{item: _event.data.item, price: _event.data.price}])"/>
<assign location="total" expr="total + _event.data.price"/>
</transition>
<transition event="checkout" cond="lines.length > 0" target="paying">
<assign location="attempts" expr="0"/>
<assign location="problem" expr="null"/>
</transition>
</state>
<!-- The order asks the payment service to charge the total, and waits.
A declined card sends it back to the cart; a service that cannot be
reached is tried again, three times in all. -->
<state id="paying">
<onentry>
<assign location="attempts" expr="attempts + 1"/>
<send type="payments" event="charge">
<param name="amount" expr="total"/>
<param name="items" expr="lines.map((line) => line.item)"/>
</send>
</onentry>
<transition event="charged" target="paid"/>
<transition event="declined" target="cart">
<assign location="problem" expr="_event.data.reason"/>
</transition>
<transition event="error.communication" cond="attempts < 3" target="retrying"/>
<transition event="error.communication" target="cart">
<assign location="problem" expr="'the payment service could not be reached'"/>
</transition>
</state>
<!-- The payment service could not be reached: the order waits a second,
then charges again, on its own. -->
<state id="retrying">
<onentry>
<log label="order" expr="'payment service unreachable, attempt ' + attempts + ': retrying in 1 s'"/>
<send event="retry" delay="1s"/>
</onentry>
<transition event="retry" target="paying"/>
</state>
<!-- The order is paid: its total is what it hands back as it ends. -->
<final id="paid">
<donedata><content expr="total"/></donedata>
</final>
</scxml>charge {"amount":42,"items":["notebook","pen"]}
log payment service unreachable, attempt 1: retrying in 1 s
charge {"amount":42,"items":["notebook","pen"]}
paid 42The first charge fails, so the chart waits a second and charges again. Your program never wrote that retry: it lives in the chart's retrying state.
Every package prints these same lines. The chart's expressions are JavaScript in every package, Python's included.
The chart decides, your program acts
- The hostruns the actors inside your process, so there is no server to start. Its system id,
shop, names it in actors' addresses, such astinyactors://shop/@order-17. - The chartholds every state the order can be in and the events that move it. Press the buttons under the diagram to drive it yourself.
- The I/O processoris the
paymentsobject your program gives the host. The chart sends itchargeand never calls a network itself, so a test can answer instead of a real service. The payment retry test lets three attempts time out by stepping the clock past each wait. - A crashstops the actors in the process, the order waiting for its retry included. Journal and replay and snapshots bring them back.
Let your coding agent read the docs
Add the tinyactors support server to your agent, and it can search these docs, load the skill for building with tinyactors and report what got in its way. In Claude Code:
claude mcp add --transport http tinyactors-support https://mcp.tinyactors.dev/mcpBuild with a coding agent covers the other agents and what the server offers.
Go further
- Statecharts and actors in ten minutesthe ideas behind the chart above, with an order you can drive.
- Build a team chata tutorial of nine steps, from one room to direct messages, each a program that runs.
- Test your chartsa week of timeouts in milliseconds on a simulated clock.
- All guideshibernation, migration, snapshots, journals and the rest, each with a program that runs.