Durable agent loops: every LanguageModel request and every tool handler run inside
a workflow becomes one of its activities, so a crashed run resumes where it stopped::
AgentLive = Agent.to_layer(run_agent).pipe(
fx.layer.provide_merge(durable.layer),
# then the LanguageModel, the toolkit's handlers and the workflow engine
)The agent's code does not change. Outside a workflow, and inside an activity (whose result
already covers what it does), layer changes nothing.
Each step's activity is named by its place in the run, so a replay finds it again:
pyfect/ai/<n>/model for the run's n-th model request, and
pyfect/ai/<n>/tool/<i> for the i-th tool call of that request's response (both
from 1). A tool call that no response of the run made (a call WithHandler.handle was
given directly) is pyfect/ai/handler/<k>, the run's k-th. The numbers come in the
order the run makes its requests, so model requests a run makes concurrently need that
order to be the same on every replay.
An activity stores the step's output and a hash of its request: the prompt, the tools
offered, the tool choice and the response format, or the tool's name and the parameters
the model sent. The prompt itself is not stored (the run rebuilds it on replay), so what
a run stores grows with each step's output, not with the conversation so far. A replay
whose request hashes differently from the one stored dies with RequestDiverged: the
code changed under a running execution, and reusing the stored output would answer a
different question. Annotate the workflow with workflow.SuspendOnFailure to keep such
an execution resumable after a rollback.
A streamed request is one activity too: the provider's stream is read to its end, and the caller gets its parts as one chunk, on the first run and on every replay alike, so a consumer never sees a part twice when a crash cuts a stream short.
Values#
| Attribute | Type | Description |
|---|---|---|
layer | Layer[lm.LanguageModel, Never, lm.LanguageModel] | The |
Classes#
Functions#
request_hash#
request_hash(options, *, streamed) -> strThe hash a model request is stored with: its prompt, tools, tool choice and response format, and whether it was streamed.
| Parameter | Type | Description |
|---|---|---|
options | lm.ProviderOptions | |
streamed | bool |
Returns str
def request_hash(options: lm.ProviderOptions, *, streamed: bool) -> str:
"""The hash a model request is stored with: its prompt, tools, tool choice and
response format, and whether it was streamed."""
response_format: object = (
{"type": "text"}
if isinstance(options.response_format, lm.TextFormat)
else {
"type": "json",
"name": options.response_format.object_name,
"schema": get_json_schema_from_schema(options.response_format.schema),
}
)
return _digest(
{
"streamed": streamed,
"prompt": _prompt_digest(options.prompt.content),
"tools": [_tool_document(tool) for tool in options.tools],
"toolChoice": options.tool_choice,
"responseFormat": response_format,
}
)call_hash#
call_hash(tool_name, params) -> strThe hash a tool call is stored with: the tool's name and the parameters the model sent.
| Parameter | Type | Description |
|---|---|---|
tool_name | str | |
params | object |
Returns str
def call_hash(tool_name: str, params: object) -> str:
"""The hash a tool call is stored with: the tool's name and the parameters the model
sent."""
return _digest({"tool": tool_name, "params": params})