Skip to main content

State machine orchestration for agent workflows.

Project description

FlatMachines (Python SDK)

State-machine orchestration for LLM agents. FlatMachines is agent-framework agnostic and uses adapters to execute agents from FlatAgents, smolagents, pi-mono, or other runtimes.

For LLM/machine readers: see MACHINES.md.

Install

pip install flatmachines[flatagents]

Optional extras:

  • flatmachines[cel] – CEL expression engine
  • flatmachines[validation] – JSON schema validation
  • flatmachines[metrics] – OpenTelemetry metrics
  • flatmachines[smolagents] – smolagents adapter

Quick Start

from flatmachines import FlatMachine

machine = FlatMachine(config_file="workflow.yml")
result = await machine.execute(input={"query": "..."})
print(result)

workflow.yml

spec: flatmachine
spec_version: "0.10.0"

data:
  name: reviewer
  agents:
    reviewer: ./reviewer.yml
  states:
    start:
      type: initial
      agent: reviewer
      input:
        code: "{{ input.code }}"
      output_to_context:
        review: "{{ output }}"
      transitions:
        - to: done
    done:
      type: final
      output:
        review: "{{ context.review }}"

Agent Adapters

FlatMachines delegates agent execution to adapters. Built-ins are registered automatically if their dependencies are installed:

  • flatagent – uses FlatAgents configs (flatmachines[flatagents])
  • smolagents – executes MultiStepAgent via agent_ref.ref factories
  • pi-agent – Node bridge to pi-mono using pi_agent_runner.mjs

Agent refs in data.agents can be:

  • string path to a flatagent config
  • inline flatagent config (spec: flatagent)
  • typed adapter ref: { type: "smolagents" | "pi-agent" | "flatagent", ref?: "...", config?: {...} }

Custom adapters can be registered via AgentAdapterRegistry.

State Execution Order

For each state, the Python runtime executes in this order:

  1. actionhooks.on_action
  2. launch → fire-and-forget machine(s)
  3. machine / foreach → peer machines (blocking)
  4. agent → adapter + execution strategy
  5. output → render final output for type: final

Jinja2 templates render input, context, and output. A plain string like context.foo resolves to the actual value (not a string).

Execution Types

execution:
  type: retry
  backoffs: [2, 8, 16]
  jitter: 0.1

Supported: default, retry, parallel, mdap_voting.

Parallelism & Launching

  • machine: child → invoke peer machine (blocking)
  • machine: [a, b] → parallel
  • foreach: "{{ context.items }}" → dynamic parallelism
  • launch: child → fire-and-forget; result is written to backend

Persistence & Resume

persistence:
  enabled: true
  backend: local  # local | memory
  checkpoint_on: [machine_start, state_enter, execute, state_exit, machine_end]

Resume with machine.execute(resume_from=execution_id). Checkpoints store MachineSnapshot including pending launches.

Hooks

Configure hooks via data.hooks:

hooks:
  file: ./hooks.py
  class: MyHooks
  args: { ... }

Built-ins: LoggingHooks, MetricsHooks, WebhookHooks, CompositeHooks.

Invokers & Result Backends

Invokers define how peer machines are launched:

  • InlineInvoker (default)
  • QueueInvoker (base class for external queues)
  • SubprocessInvoker (python -m flatmachines.run)

Results are written/read via ResultBackend URIs: flatagents://{execution_id}/result.

Logging & Metrics

from flatmachines import setup_logging, get_logger
setup_logging(level="INFO")
logger = get_logger(__name__)

Env vars match FlatAgents: FLATAGENTS_LOG_LEVEL, FLATAGENTS_LOG_FORMAT, FLATAGENTS_LOG_DIR.

Metrics require flatmachines[metrics] and FLATAGENTS_METRICS_ENABLED=true.

CLI Runner

python -m flatmachines.run --config machine.yml --input '{"key": "value"}'

Examples (Repo)

Specs

Source of truth:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flatmachines-2.2.1.tar.gz (103.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flatmachines-2.2.1-py3-none-any.whl (126.8 kB view details)

Uploaded Python 3

File details

Details for the file flatmachines-2.2.1.tar.gz.

File metadata

  • Download URL: flatmachines-2.2.1.tar.gz
  • Upload date:
  • Size: 103.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for flatmachines-2.2.1.tar.gz
Algorithm Hash digest
SHA256 e2eaecf83139796483c1585bf06c6fdc981cea9e6baed132f5411e7b6f3cecc5
MD5 90256b342e4f28ca350bc38e5d0beb85
BLAKE2b-256 db787e9c8c450ea59f31a9adef42d7e2f8073780dea6e7938d053839c6754fc1

See more details on using hashes here.

File details

Details for the file flatmachines-2.2.1-py3-none-any.whl.

File metadata

  • Download URL: flatmachines-2.2.1-py3-none-any.whl
  • Upload date:
  • Size: 126.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for flatmachines-2.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 145544801a245356e7abe84410be84c75e757555462eb0f1173bf8a915ca4f9b
MD5 61eb1561a06b1c6ef4fdbc35bfd742c6
BLAKE2b-256 a5fbe4996c11aadcb9fda845407e7d34ea6eae941ef2b76a63bc407a60d5e4f8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page