Skip to main content
Everything is importable from the top-level package:
from cadenza_cli import (
    MeganTK, MeganSession, Anticipator,
    Decision, ProtectResult,
    MeganTKError, AuthRequired,
)

MeganTK

The client, bound to one account’s token. Construct once and reuse.
MeganTK(api_key=None, *, api_url=None, timeout=30.0)
ArgumentTypeDefaultPurpose
api_keystr | NoneresolvedThe token. If None, resolved from CADENZA_API_KEY, CADENZA_TOKEN, then the CLI session.
api_urlstr | Nonehosted APIBase URL override. Falls back to CADENZA_API_URL, then https://www.api.cadenzalabs.xyz.
timeoutfloat30.0Per-request timeout, seconds.
Raises AuthRequired at construction if no key can be resolved.

Methods

MethodReturnsDescription
version()dict{service, megantk_version, api_version}. Open route — no auth, not metered.
ping()boolTrue if the token authenticates (raises otherwise).
usage(days=30)dictYour metered consumption. See Usage.
session(objective, milestones, *, ring_radii=None, obstacle_block_radius=0.8, config=None)MeganSessionOpen a token session.
anticipator(actions=(), *, lead=1.4, guard=0.8, min_events=2)AnticipatorOpen a disturbance anticipator.

session(...)

ArgumentTypeDefaultPurpose
objectivestrThe goal the token pursues.
milestonesSequence[str]Ordered milestone chain (at least one).
ring_radiiSequence[float] | NoneNoneOne shrinking radius per milestone. Provide to enable perceive.
obstacle_block_radiusfloat0.8How close an obstacle must be to block (perception path).
configdict | NoneNoneOptional MeganToken config overrides.

anticipator(...)

ArgumentTypeDefaultPurpose
actionsSequence[str]()Protective action names to register.
leadfloat1.4Seconds to start protecting before a predicted hit.
guardfloat0.8Seconds to keep protecting after.
min_eventsint2Disturbances observed before the period is trusted.

MeganSession

A live token session. Returned by MeganTK.session(). A context managerclose() runs on __exit__. Attributes: session_id: str, objective: str, milestones: list[str], perception: bool.
MethodReturnsDescription
step(reached=(), obstacles=())DecisionMilestone path. reached: milestone indices satisfied. obstacles: {"id","milestone"} dicts or (id, index) tuples.
perceive(position, goal, obstacles=())DecisionPerception path (session must have ring_radii). obstacles: {"id","position"} dicts or (id, position) tuples.
status()dict{session_id, session_step, objective, perception}.
close()boolDelete server-side. Idempotent — True first time, False after.

Anticipator

A live disturbance anticipator. Returned by MeganTK.anticipator(). A context manager. Attributes: anticipator_id: str, actions: list[str].
MethodReturnsDescription
disturbance(t)dictLog a disturbance at time t (seconds). Returns {anticipator_id, ready, best_action}.
outcome(name, saved)dictRecord whether registered action name preserved the outcome.
protect(t)ProtectResultShould you protect at time t?
status()dict{anticipator_id, ready, best_action}.
close()boolDelete server-side. Idempotent.

Decision

Frozen value object returned by step / perceive.
FieldTypeMeaning
routestr'continue' or 'adapt'.
frontierstr | NoneThe milestone attention is on (e.g. 'milestone_1').
session_stepintMonotonic step counter.
adaptbool (property)True when route == 'adapt'.

ProtectResult

Frozen value object returned by protect.
FieldTypeMeaning
should_protectboolEngage a protective action now?
best_actionstr | NoneWhich registered action is currently best.
readyboolWhether the rhythm is learned enough to act on.

Errors

from cadenza_cli import MeganTKError, AuthRequired
ExceptionRaised when
AuthRequired (subclass of MeganTKError)No key could be resolved, or the server returned 401/403 (invalid/revoked token, deactivated account).
MeganTKErrorAny other API problem — bad request (422), rate limit (429), not found (404), or an unreachable API. The message is the server’s human-readable detail.
from cadenza_cli import MeganTK, AuthRequired, MeganTKError

try:
    tk = MeganTK()
    d = tk.session("goal", ["a", "b"]).step(reached=[0])
except AuthRequired as e:
    ...   # fix the key / sign in
except MeganTKError as e:
    ...   # transient / bad input — inspect str(e)
See Building deep for retry and rate-limit patterns.