The change-doer pattern
Keep three responsibilities separate:1
Sense
Your perception produces the state megan-tk needs — which milestones are
reached, what’s blocking, or raw position/goal/obstacles.
2
Govern (megan-tk)
step / perceive returns a Decision. This is the only thing megan-tk
does — it does not move the robot.3
Change (you)
On
adapt, your change-doer intervenes at frontier — re-plan, switch
skill, correct a trajectory. On continue, let the policy run.Server-held state and the control loop
The server holds the live token object — its knowledge graph accumulates across steps. Two consequences:- One session per task attempt. Open it once, step it every cycle, close it at the end. Don’t open a fresh session per tick.
- Close what you open. Sessions and anticipators persist server-side until
deleted. The
withform guarantees cleanup; if you hold one by hand, close it in afinally.
Rate limits
megan-tk routes are rate-limited per account (default 120 requests/minute). This is deliberate — it keeps the decision boundary from being reconstructed by mass probing. Design your loop to stay under it.Error handling
Two exception types, one narrower than the other:A 422 means your input is wrong (an out-of-range milestone index,
perceive
on a non-perception session, an unregistered action name). These won’t fix
themselves on retry — correct the call.Composing sessions and anticipators
The two primitives are independent and compose in a single loop: a session governs task progress while an anticipator guards against a periodic disturbance.Becoming more efficient
Efficiency is Megan’s on-device self-improvement layer — the third of the three ways a robot self-learns. Once a task reliably succeeds, it makes that task faster, one committed step at a time, without ever destabilising the outcome.This layer runs on the robot as part of the change-doer — it is not a hosted
API route you call each tick. It surfaces as your repeated task simply getting
faster while staying stable. You observe it in your own outcome metrics (task
duration trending down, the result staying within tolerance).
1
Set a golden reference
The first, deliberately-slow run of the task settles into a reference outcome —
the state that must stay stable (object positions, an end pose, a metric).
2
Change one thing per repetition
Each later rep nudges a single thing a little: speed up the slowest step, or
drop a step your rule marks redundant.
3
Commit or revert
Keep the change only if the outcome still matches golden within tolerance and
the rep was faster; otherwise revert and stop pushing that step. Learned
speeds persist, so the robot keeps its gains across sessions.
Deployment checklist
1
Key via environment
Set
CADENZA_API_KEY in the deploy environment; don’t hard-code the token.2
Reuse one client
Construct
MeganTK once at startup and share it — it holds no live
connection but does resolve the key at construction.3
Bound your call rate
Decimate governance calls to stay under 120/min per account.
4
Handle both error types
Fail fast on
AuthRequired; back off + retry transient MeganTKError.5
Close sessions
Use
with, or close() in finally, so server-side state doesn’t leak.6
Watch your usage
cadenza usage / tk.usage() to confirm call volume and catch runaway loops.Full example
A complete, runnable end-to-end script that exercises every route lives in the repo atexamples/megantk_e2e.py,
and a compact quickstart at
examples/megantk_quickstart.py.
Next
SDK reference
Every method and type in one place.
Usage & metering
Track what your loop is consuming.