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
string
required
The workflow to invoke, in the format
workflow:<ref_name>.array
An array of chat messages (OpenAI format). Required unless
input is provided.object | string | array
Arbitrary JSON payload passed to the workflow. Required unless
messages is provided. Use this for Lambda-style invocations where chat messages don’t apply.string
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.
boolean
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.object
Optional key-value pairs available to the workflow at runtime (e.g. API keys the workflow needs to call external services). Not stored.
object
Optional metadata passed to the workflow.
Response format
Workflows return a standard OpenAI-compatiblechat.completion response:
string
Unique identifier for the completion.
string
The workflow that was invoked.
array
Array containing the workflow output.
choices[0].message.content holds the result.string
Maitai request identifier, useful for debugging.
Authentication
Workflows use the same API key authentication as all Maitai endpoints. See Authentication.Error handling
Error responses follow the standard format:
Next
- Write your first workflow: Workflow Structure
- The full runtime API: Workflow Context (
ctx) - Attach reference data and upload: Datastores & Accessories