GitHub

tool_ledger

API reference for tool_ledger.

A ledger of tool calls, so a replayed call does not run its handler twice.

guard(toolkit, on_unknown=...) gives the toolkit back with every handler behind a ToolLedger, keyed by the call's tool_call_id. Before a handler runs, the ledger records the intent to run it; when the handler has given its final result, the ledger records that result. A call the ledger has seen before is answered from it:

  • a recorded result is returned as it is, and the handler does not run;
  • an intent without a result (the run before was interrupted, or its process died) runs again only for a tool annotated Idempotent or Readonly. Any other tool does not run: the call's outcome is unknown, and on_unknown says what happens. With "tell_model" the model gets a failed tool result holding a ToolOutcomeUnknown, so the conversation stays valid; with "fail" the call fails with ToolOutcomeUnknown, for a person to decide.

The guarantee is at most once, or you are told: a handler runs at most once per tool_call_id (idempotent and read-only tools aside), and when the ledger cannot say whether it ran, you get ToolOutcomeUnknown. It is not exactly once: a handler interrupted mid-way may have done part of its work, and one that finished but whose result was not recorded did all of it.

layer_memory keeps the ledger in the process; layer_sql keeps it in a table of the SqlClient, so it outlives the process.

Values

AttributeTypeDescription
layer_memoryLayer[ToolLedger]

A ToolLedger in this process, new for each build: it survives an interrupt or a retry, but not the process.

Types

TypeDefinitionDescription
OnUnknowntype OnUnknown = Literal['tell_model', 'fail']

What a call whose outcome is unknown does: "tell_model" answers the model with a failed tool result holding ToolOutcomeUnknown; "fail" fails the call with it.

Entrytype Entry = Pending | Completed

What the ledger holds for a call.

Beguntype Begun = Claimed | Entry

What begin found: nothing (now Claimed), or the call's entry, unchanged.

Classes

Functions

memory

memory() -> ToolLedgerService

A new, empty ledger in this process.

Returns ToolLedgerService

def memory() -> ToolLedgerService:
    """A new, empty ledger in this process."""
    return _MemoryLedger()

layer_sql

layer_sql(*, table='effect_ai_tool_ledger') -> Layer[ToolLedger, Never, SqlClient]

A ToolLedger in table (made if missing) of the SqlClient: one row per tool call, the result as JSON text once there is one.

ParameterTypeDescription
tablestr = 'effect_ai_tool_ledger'

Returns Layer[ToolLedger, Never, SqlClient]

def layer_sql(*, table: str = "effect_ai_tool_ledger") -> Layer[ToolLedger, Never, SqlClient]:
    """A ``ToolLedger`` in ``table`` (made if missing) of the ``SqlClient``: one row per
    tool call, the result as JSON text once there is one."""
    return layer_effect(ToolLedger, fx.gen(lambda: _build_sql(table)))

guard

guard[N: LiteralString, RP, HE, R, EE, RR](toolkit: Effect[WithHandler[N, RP, HE, R], EE, RR], *, on_unknown: Literal['tell_model']) -> Effect[WithHandler[N, RP | ToolResultFailure[N, ToolOutcomeUnknown], HE | ToolLedgerError, R], EE, RR | ToolLedger]
guard[N: LiteralString, RP, HE, R, EE, RR](toolkit: Effect[WithHandler[N, RP, HE, R], EE, RR], *, on_unknown: Literal['fail']) -> Effect[WithHandler[N, RP, HE | ToolLedgerError | ToolOutcomeUnknown, R], EE, RR | ToolLedger]

toolkit with its calls recorded in the ToolLedger (see the module docstring). Pass it as toolkit= where the toolkit would go.

ParameterTypeDescription
toolkit
on_unknown
@overload
def guard[N: LiteralString, RP, HE, R, EE, RR](
    toolkit: Effect[WithHandler[N, RP, HE, R], EE, RR], *, on_unknown: Literal["tell_model"]
) -> Effect[
    WithHandler[N, RP | ToolResultFailure[N, ToolOutcomeUnknown], HE | ToolLedgerError, R],
    EE,
    RR | ToolLedger,
]: ...
@overload
def guard[N: LiteralString, RP, HE, R, EE, RR](
    toolkit: Effect[WithHandler[N, RP, HE, R], EE, RR], *, on_unknown: Literal["fail"]
) -> Effect[
    WithHandler[N, RP, HE | ToolLedgerError | ToolOutcomeUnknown, R], EE, RR | ToolLedger
]: ...
def guard(toolkit: Effect[Any, Any, Any], *, on_unknown: OnUnknown) -> Effect[Any, Any, Any]:
    """``toolkit`` with its calls recorded in the ``ToolLedger`` (see the module
    docstring). Pass it as ``toolkit=`` where the toolkit would go."""
    require_effect(toolkit, "toolkit")

    def wrap(kit: WithHandler[Any, Any, Any, Any], ledger: ToolLedgerService) -> _Guarded:
        return _Guarded(kit.tools, kit.services, kit, ledger, on_unknown)

    return ToolLedger().flat_map(lambda ledger: toolkit.map(lambda kit: wrap(kit, ledger)))