Klarety

Automations and runs

Save, test, start, follow, approve and stop automation runs from Python.

For devs

An is a saved set of steps that works on your data, on a schedule or when something happens. Every automation belongs to a . New to automations? Start with Create an automation.

Before you start

  • You have the project ID of the project the automation lives in, and you can edit that project.
  • Taking an automation live needs a owner or admin, because a live automation spends .
  • Every example assumes nexus = Nexus.connect().

Find automations

for agent in nexus.automations.list(project_id):
    print(agent)

for agent in nexus.automations.list_all():   # every project you can open
    print(agent)

Save and test a draft

An automation is a file automations/<name>.yaml with its Python code in automations/<name>/, inside your project's code. save() stores both from a local folder as a draft:

test_agent.py
from pathlib import Path

from klarety import KlaretyError, Nexus

PROJECT = "your-project-id"

with Nexus.connect() as nexus:
    try:
        version_id = nexus.automations.save(PROJECT, "daily-summary", root=Path("."))
        run_id = nexus.automations.start(PROJECT, "daily-summary", draft=True, inputs={"days": 1})
    except KlaretyError as failed:
        raise SystemExit(f"Could not test the agent: {failed}")
    print("Draft", version_id, "running as", run_id)

nexus.automations.check(project_id, text) checks the YAML text without saving it. A draft never runs on its own; start(..., draft=True) runs it once.

Take it live

nexus.automations.ask_to_publish(PROJECT, "daily-summary", version_id)

A person decides in the app. An owner or admin can publish directly with nexus.automations.publish(PROJECT, "daily-summary", version_id); anyone else is refused. unpublish() takes it offline, and publish() with an earlier version_id brings that version back. nexus.automations.version(project_id, version_id) shows a saved version.

Start a run and follow it

Each reports its state and steps:

run_and_wait.py
import time

from klarety import Nexus

PROJECT = "your-project-id"

with Nexus.connect() as nexus:
    run_id = nexus.automations.start(PROJECT, "daily-summary")
    run = nexus.automations.run(PROJECT, run_id)
    while run["state"] == "running":
        time.sleep(10)
        run = nexus.automations.run(PROJECT, run_id)

    print(run["state"], run["reason"], run["credits"], "credits")
    for step in run["steps"]:
        print(step["id"], step["state"], step["reason"])

A run ends as done, failed, declined or stopped. Each step has its own state: waiting, running, asking, done, failed, declined, stopped or skipped, among others.

Answer an approval step

A step in the asking state waits for a person to say yes:

for step in run["steps"]:
    if step["state"] == "asking":
        nexus.automations.answer(PROJECT, run_id, step["id"], yes=True)

yes=True continues the run. yes=False stops it at that step.

Read what a run produced

result = nexus.automations.result(PROJECT, run_id)
print(result["value"])         # up to 1 MiB
print(result["files"])         # files the run left

names = nexus.automations.outputs(PROJECT, run_id, step_id)
data = nexus.automations.output(PROJECT, run_id, step_id, names[0])   # bytes
print(nexus.automations.log(PROJECT, run_id, step_id))

For a step that runs once per item, pass item= to outputs(), output() and log() to read one item.

List runs, stop and rerun

for one in nexus.automations.runs(PROJECT, agent="daily-summary"):   # newest first, page by page
    print(one["run_id"], one["state"])

nexus.automations.stop(PROJECT, run_id)
new_run = nexus.automations.rerun(PROJECT, run_id, from_step="summarise")
print(nexus.automations.stats(PROJECT, days=7))   # runs per day: count, outcomes, cost

rerun() starts a new run from one step, reusing earlier steps' output. To page from another process, use runs_page() and pass its next back as after.

Delete an automation

nexus.automations.delete(PROJECT, "daily-summary")

The automation is paused and removed from the list. Its runs stay on record.

What can go wrong

ExceptionCauseWhat to do
KlaretyError: automations/<name>.yaml does not existsave() could not find the file under root.Run from the project's folder or pass root=.
RefusedErrorYou cannot edit the project, or you are not an admin and tried to publish().Use ask_to_publish(), or ask for access.
NotFoundErrorNo such project, automation or run.Check the IDs.
InsufficientCreditsErrorThe workspace cannot pay for the run.Add credits.

Next steps

On this page