Record each tool call before and after its handler runs, so a replayed call does not pay twice.
A tool call can reach its handler more than once. A turn is retried after a timeout, a process dies and its work is resumed, an approved call is run again from the chat's history. With a plain toolkit, each replay runs the handler again. That is harmless for a weather lookup and a real problem for a payment or an email.
pyfect.ai.tool_ledger puts a ledger in front of the handlers. tool_ledger.guard(Tools, on_unknown=...) gives back the toolkit, to pass as toolkit= wherever the toolkit
would go. For each call, keyed by its tool_call_id, the ledger writes two records:
- Before the handler runs, an intent: this call has started.
- After the handler's final result, that result, as the model saw it.
When the same call comes again, the ledger answers first:
- A call with a recorded result gets that result back. The handler does not run.
- A call with an intent and no result was cut short somewhere between the two records, so
the ledger cannot know whether the handler did its work. A tool annotated
IdempotentorReadonly(Pay.annotate(tool.Idempotent, True)) runs again. Any other tool does not run, and its outcome is unknown.
At most once, or you are told#
The guarantee is: a handler runs at most once per tool_call_id, and when the ledger
cannot tell whether it ran, you are told, with a ToolOutcomeUnknown. Idempotent and
read-only tools are the exception, because they are allowed to run again.
It is not exactly once. A handler interrupted half way may have done part of its work. A handler that finished but whose result was never recorded did all of it, and its replay still reports the outcome as unknown. Only the tool's own system can say what happened: make the call idempotent there (a payment API's idempotency key, say) when you need more than this.
Choosing what an unknown outcome does#
on_unknown has no default, because the right answer depends on the tool:
"tell_model"answers the call with a failed tool result holdingToolOutcomeUnknown: itstoolCallId,toolNameand adescriptiontelling the model not to assume the call succeeded or failed. The call still gets a result, so the history stays valid for every provider, and the model can ask the user or check another way. The response'stool_resultstype includesToolResultFailure[N, ToolOutcomeUnknown]."fail"fails the turn withToolOutcomeUnknownas a typed error, for code or a person to decide. It is in the error type ofgenerate_text, so pyright makes you handle it.
Either way the turn's errors also include ToolLedgerError: the ledger could not read or
write a record. A failed intent write stops the call before its handler. A failed result
write fails the call after its handler ran, and the call stays pending.
The handler's own typed failures are outcomes too: the ledger records them and replays them
as the tool's failure_mode says, without running the handler. A defect or an interrupt
records nothing, so the call stays pending. Calls whose parameters do not decode never
reach the handler, and leave no record.
import tempfile
from pathlib import Path
from typing import Any, TypedDict
import pyfect as fx
from pyfect import schema as S
from pyfect import stream
from pyfect.ai import language_model, tool, tool_ledger, toolkit
from pyfect.ai.language_model import LanguageModel, ProviderOptions
from pyfect.sql import sqlite
from pyfect.sql.error import SqlError
class Payment(TypedDict):
amount: int
class Receipt(TypedDict):
receipt: str
Pay = tool.make(
"Pay",
description="Pay an amount",
parameters=S.Struct(Payment),
success=S.Struct(Receipt),
)
Tools = toolkit.make(Pay)
paid: list[int] = []
def charge(payment: Payment) -> Receipt:
paid.append(payment["amount"])
return Receipt(receipt=f"r-{len(paid)}")
PayLive = Tools.to_layer(Pay.handler(lambda params, _ctx: fx.sync(lambda: charge(params))))
# The guarded toolkit: an unknown outcome goes to the model as a tool result.
Guarded = tool_ledger.guard(Tools, on_unknown="tell_model")
@fx.fn
def turn():
reply = yield from language_model.generate_text("Pay 5", toolkit=Guarded)
for result in reply.tool_results:
print(result.name, "failed" if result.is_failure else "ok", result.encoded_result)
# A scripted model that asks for the same call each time, as a replayed turn does.
def answer(_options: ProviderOptions):
call = {"type": "tool-call", "id": "pay-1", "name": "Pay", "params": {"amount": 5}}
usage = {"inputTokens": {"total": 9}, "outputTokens": {"total": 4}}
return fx.succeed([call, {"type": "finish", "reason": "tool-calls", "usage": usage}])
Scripted = fx.layer.effect(
LanguageModel, language_model.make(generate_text=answer, stream_text=lambda _: stream.empty)
)
def ledger_in(database: Path) -> fx.Layer[tool_ledger.ToolLedger, SqlError]:
client = sqlite.layer(sqlite.SqliteClientConfig(filename=str(database)))
return tool_ledger.layer_sql().pipe(fx.provide(client))
def run(program: fx.Effect[None, Any, Any], database: Path) -> None:
provided = program.pipe(fx.provide(ledger_in(database)))
fx.run_blocking(provided.pipe(fx.provide(PayLive)).pipe(fx.provide(Scripted)).or_die())
with tempfile.TemporaryDirectory() as directory:
# Run the turn, then replay it in a fresh runtime: the second run is answered
# from the ledger, and pays nothing.
database = Path(directory) / "ledger.db"
run(turn(), database)
run(turn(), database)
assert paid == [5]
# A process that died after recording the intent, before the result: the
# replay does not pay again, and the model is told the outcome is unknown.
crashed = Path(directory) / "crashed.db"
started = tool_ledger.ToolLedger().flat_map(lambda ledger: ledger.begin("pay-1", "Pay"))
run(started.as_void(), crashed)
run(turn(), crashed)
assert paid == [5]Stores#
tool_ledger.layer_memory keeps the ledger in the process. It protects against retries
and interrupts, not against the process dying. tool_ledger.layer_sql(table=...) keeps it
in a table of the SqlClient (made if missing, effect_ai_tool_ledger by
default), on SQLite, PostgreSQL, MySQL or SQL Server. A fresh process over the same database
sees every record. Recording the intent is a single insert-if-absent statement, so two
processes replaying the same call at once never both run it.
The ledger is the ToolLedger service, whose ToolLedgerService has three operations:
begin (record the intent unless the call has a record, and say which it was), complete
and lookup. Any other store can implement them.
What the ledger does not cover#
The ledger knows a call by its tool_call_id, and the model makes that id. A model asked
again makes new calls with new ids, which are new calls to the ledger. So the ledger
protects a replay of the same call: an approved call run again from the history, a
response that was recorded before its tools ran, a retried tool resolution. A turn that
asks the model again is not one of them.
Ids are compared exactly. On SQL Server, whose primary keys ignore trailing spaces, a
second id that differs from a stored one only in trailing spaces fails with
ToolLedgerError and does not run.