Context¶
Langclaw has two distinct context objects — don't confuse them:
LangclawContext— per-request channel metadata (who/where a message came from), available to tools and middleware.WorkflowContext— thectxhanded to a@app.workflow()body; the step surface (tool,llm,subagent,agent,parallel,phase,log).
LangclawContext¶
langclaw.LangclawContext(*, user_role='viewer', channel='', user_id='', context_id='', chat_id='', metadata=dict())
dataclass
¶
Runtime context schema passed to every agent invocation.
Centralises channel metadata and RBAC role so middleware, tools,
and user code can all read runtime.context uniformly.
WorkflowContext¶
The object every workflow body receives as its first argument
(async def (ctx, inp) -> output). See the Workflows guide for usage.
langclaw.WorkflowContext(*, executor, memoize=None, phase_cb=None, log_cb=None, max_steps=1000, semaphore=None, _phase='default', _prefix='', _counter=None)
¶
Step surface for a workflow body.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
executor
|
StepExecutor
|
Async callable that performs a :class: |
required |
memoize
|
Callable[[StepRequest, Callable[[], Awaitable[Any]]], Awaitable[Any]] | None
|
Optional async callable |
None
|
phase_cb
|
PhaseCallback | None
|
Optional callback invoked on each :meth: |
None
|
log_cb
|
LogCallback | None
|
Optional callback invoked on each :meth: |
None
|
max_steps
|
int
|
Step-count backstop for the whole run. |
1000
|
semaphore
|
Semaphore | None
|
Shared concurrency limiter for |
None
|
The leading-underscore _phase / _prefix / _counter parameters are
internal deterministic-ID state, also used to spawn child contexts inside
:meth:parallel.
Source code in langclaw/workflows/context.py
agent(name, prompt, *, schema=None)
async
¶
Run a step against a named agent, returning its reply.
When schema is given the reply is validated against it; a validation
failure raises :class:WorkflowStepError.
Source code in langclaw/workflows/context.py
llm(prompt, *, schema=None, model=None, system=None)
async
¶
Make a single model call — no tools, no agent loop.
The lightweight third option beside :meth:tool (a deterministic
capability) and :meth:subagent (an autonomous worker): a bare LLM call
for one-shot judgment — classify, score, extract, rewrite, summarise.
Like every step it is memoised and crash-resumable, so a bare model call becomes a first-class, durable workflow step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The user prompt. |
required |
schema
|
type | None
|
Optional Pydantic model. When given, the reply is validated structured output (one structured call); when omitted, plain text. |
None
|
model
|
str | None
|
Optional model spec (e.g. |
None
|
system
|
str | None
|
Optional system instruction. |
None
|
Source code in langclaw/workflows/context.py
log(message)
¶
Emit a free-text progress line (e.g. "3/10 sources fetched").
Routed to the injected log_cb (typically projected to the invoking
channel as a progress event). A no-op when no callback is installed,
so a workflow body can narrate freely without caring whether anyone is
listening.
Source code in langclaw/workflows/context.py
parallel(thunks, *, return_exceptions=False)
async
¶
Run thunks concurrently, bounded by the run's concurrency budget.
Each thunk receives its own child context whose step IDs are prefixed by the branch index, so IDs stay deterministic across re-runs (required for durable resume). Returns results in input order.
By default this is fail-fast: if any branch raises, the exception
propagates out of the batch (matching asyncio.gather). Pass
return_exceptions=True to instead collect each failure in place —
a branch that raises yields its exception object in the results list
while its siblings still complete. Filter survivors with
[r for r in results if not isinstance(r, Exception)].
Source code in langclaw/workflows/context.py
phase(name)
¶
Start a new named phase; subsequent steps are grouped under it.