What a workflow is
A workflow is one Python file with anexecute(ctx) function. Maitai calls execute, passes it a context object, and returns whatever you return.
ctx object is the workflow’s connection to everything: its input, Maitai model and agent calls, arbitrary HTTP, queryable datastores, streaming, and logging.
Building blocks
Structure
The script layout — the
execute entrypoint, input/output schemas, and helpers.Context (ctx)
The runtime API — models, agents, HTTP, streaming, logging, and more.
Datastores & Accessories
Attach reference data — bundled files or large, queryable record stores.
Invoke
Call a workflow from the SDK or raw API, including streaming.
Lifecycle
- Write the script and (optionally) attach datastores or accessories.
- Upload it — see Uploading a workflow. Maitai registers it under a
workflow_ref_name. - Invoke it as
model="workflow:<ref_name>"(below). Maitai runs it, monitors every model call, and returns the result.
Invoke a workflow
Setmodel to workflow:<your-workflow-name>. You can pass either messages (chat-style) or input (arbitrary JSON payload), or both.
With messages (chat-style)
With input (structured payload)
For workflows that expect structured data rather than chat messages, useinput:
Raw API (cURL)
With session_id (for correlation)
Passsession_id to correlate all model calls within a workflow run in Maitai analytics:
Streaming responses
To receive workflow output in real-time as the script executes, setstream=True. The workflow will yield intermediate chunks as they are emitted (via ctx.emit()), followed by the final completion.
Passing secrets
If your workflow requires runtime credentials (e.g. third-party API keys), pass them viasecrets. These are never stored — they’re only available for the duration of the request.
Request format
The workflow to invoke, in the format
workflow:<ref_name>.An array of chat messages (OpenAI format). Required unless
input is provided.Arbitrary JSON payload passed to the workflow. Required unless
messages is provided. Use this for Lambda-style invocations where chat messages don’t apply.Optional session ID for the workflow run. If provided, overrides the auto-generated session ID. All model calls within the workflow use this session, enabling correlation across steps in Maitai analytics.
Optional. When
true, returns a stream of intermediate workflow chunks (emitted via ctx.emit()) followed by a final chat.completion object, allowing you to display real-time progress. Default false.Optional key-value pairs available to the workflow at runtime (e.g. API keys the workflow needs to call external services). Not stored.
Optional metadata passed to the workflow.
Response format
Workflows return a standard OpenAI-compatiblechat.completion response:
Unique identifier for the completion.
The workflow that was invoked.
Array containing the workflow output.
choices[0].message.content holds the result.Maitai request identifier, useful for debugging.
Authentication
Workflows use the same API key authentication as all Maitai endpoints. See Authentication.Error handling
| Status | Meaning |
|---|---|
400 | Invalid request (missing both input and messages, bad model format) |
401 | Invalid or missing API key |
404 | Workflow not found for your account |
408 | Workflow exceeded its timeout |
500 | Workflow execution error (check error.message for details) |
Next
- Write your first workflow: Workflow Structure
- The full runtime API: Workflow Context (
ctx) - Attach reference data and upload: Datastores & Accessories