> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getprovidex.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python API reference: init, session, trace and verify

> The core RootSign calls: init(), session(), @trace(), wrap_tools() and the two verify functions, with every parameter, default, return value and error.

Five calls are the stable core of RootSign: `init()`, `session()`, `@trace()`, `wrap_tools()` and verification. The planned TypeScript SDK mirrors this surface. Everything else that `rootsign` exports is lower-level and listed [at the end](#beyond-the-core-calls).

```python core.py theme={null}
import asyncio
import os

import rootsign

rootsign.init(agent="invoice-agent", risk_tier="high")


@rootsign.trace()
async def send_invoice(customer: str, amount: float) -> str:
    return f"invoice for {amount:.2f} sent to {customer}"


async def main() -> None:
    async with rootsign.session(objective="bill ACME for October") as ctx:
        print(await send_invoice("acme-corp", 1500.0))

    data_dir = os.path.expanduser(os.environ.get("ROOTSIGN_DATA_DIR", "~/.rootsign"))
    result = rootsign.verify_session_local(f"{data_dir}/sessions/{ctx.session_id}.jsonl")
    print(result.summary)


asyncio.run(main())
```

```text theme={null}
invoice for 1500.00 sent to acme-corp
VALID — 1 records, chain intact
```

## rootsign.init()

```python theme={null}
rootsign.init(
    *,
    agent=None,
    owner=None,
    environment=None,
    risk_tier=None,
    framework=None,
    description=None,
    model_version=None,
    permitted_tools=None,
    regulatory_categories=None,
) -> None
```

Configures RootSign once, at startup: which agent is recording, and the settings in force. Every argument is keyword-only and optional, so `rootsign.init()` alone is enough to record.

**agent** `str` (default: the script's file name)

The agent's name, 2 to 200 characters. Together with `environment` it identifies the agent. Without it, the name of the running script is used, or `rootsign-agent` in a notebook or REPL.

**owner** `str` (default: your operating-system user name)

The team or person accountable for the agent, 2 to 200 characters. Falls back to `unknown` when the user name is shorter.

**environment** `development | staging | production` (default: development)

Where the agent runs. The same agent name can exist separately in each environment.

**risk\_tier** `low | medium | high | critical` (default: medium)

How much harm the agent's actions can do, recorded on the agent for reviewers.

**framework** `langgraph | crewai | autogen | custom | unknown` (default: unknown)

The agent framework, recorded on the agent. It does not change how tools are wrapped.

**description** `str`

Up to 2,000 characters.

**model\_version** `str`

The model the agent uses, up to 100 characters.

**permitted\_tools** `list[str]`

Tool names the agent is meant to use, recorded on the agent.

**regulatory\_categories** `list[str]`

Labels for the rules the agent falls under, recorded on the agent.

**Returns** `None`.

**Raises** `ValueError` for a value outside the allowed list or the length limits above, or when `init()` is called again in the same process with different arguments:

```text theme={null}
rootsign.init(): agent: String should have at least 2 characters
```

**Notes**

* `init()` does no I/O. The agent is registered, in `agents.jsonl` or the PostgreSQL `agents` table, when the first session opens. Problems that need I/O, such as a missing extra or an unwritable data folder, also surface then.
* Calling `init()` again with the same arguments does nothing.
* The `ROOTSIGN_` settings are read here and stay fixed for the process. [Configuration](https://docs.getprovidex.com/reference/configuration.md#how-settings-are-read) explains when each setting is read.
* An agent's attributes are stored when it is first registered. If a later run passes different values for the same agent and environment, RootSign keeps the stored ones and logs a warning naming the difference.

> [!NOTE]
>
> The length limits apply on every backend since the release after RootSign 0.3.1. Before, the JSONL backend accepted any length, and PostgreSQL rejected the value only when the first session opened.

## rootsign.session()

```python theme={null}
async with rootsign.session(
    *,
    objective=None,
    user_id=None,
    agent_id=None,
    client=None,
) as ctx:
    ...
```

Opens a session, the unit that RootSign chains and verifies. Every traced call inside the block is recorded into it, in order. The session's open and close are recorded too.

**objective** `str`

What the agent is being asked to do. Recorded on the session.

**user\_id** `str`

The end user the agent is acting for, if your application has one.

**agent\_id** `UUID`

An agent ID from `rootsign.register_agent()`. Omit it to use the agent from `init()`.

**client** `IngestClient`

Where records are written. Omit it to use the backend from `init()`. A client that `session()` builds itself is closed when the session ends; one you pass in is left open.

**Yields** a `SessionContext`:

| Attribute | Meaning |
| - | - |
| `session_id` | The session's UUID. With the JSONL backend, the session file is `sessions/<session_id>.jsonl` in the data folder. |
| `agent_id` | The agent's UUID |
| `current_sequence` | The sequence number of the last action issued, `0` before the first |
| `record_decision(...)` | Records a decision before the next action. See [below](#ctx-record-decision). |

**Raises**

| Exception | When |
| - | - |
| `RootSignNotInitializedError` | `init()` was not called and `agent_id` and `client` were not both passed |
| `RecordPersistenceError` | The session could not be opened: the agent could not be registered, the session's first record could not be written because the data folder is not writable or the database is down, or the store refused it, for example for an agent ID it does not know. Nothing in the block runs. |
| `RootSignPostgresExtraRequired` | `ROOTSIGN_BACKEND=postgres` without `rootsign[postgres]` installed |
| `ValueError` | `ROOTSIGN_BACKEND=cloud` without `agent_id`. The hosted backend issues agent IDs, so pass the agent's ID. |

**Notes**

* If the block raises, the session is recorded as `failed` and the exception propagates. Otherwise it is recorded as `completed`.
* Traced calls find the session through a context variable, so concurrent sessions in different asyncio tasks stay separate.
* Calling a traced function after the block has ended raises `RootSignNotInitializedError`.

> [!NOTE]
>
> `RecordPersistenceError` here was added after RootSign 0.3.1. In 0.3.1 and earlier, a session that could not be opened raised the underlying `PermissionError` or `OSError`.

### ctx.record\_decision()

```python theme={null}
await ctx.record_decision(
    *,
    selected_action,
    reasoning_summary=None,
    confidence=None,
    alternatives_considered=None,
) -> UUID | None
```

Records what the agent decided, and why, and links it to the next action recorded in the session. It does nothing and returns `None` unless `ROOTSIGN_CAPTURE_DECISIONS=true`. `ROOTSIGN_REASONING_DEPTH` sets how much of the reasoning is kept; [Configuration](https://docs.getprovidex.com/reference/configuration.md#decision-capture) has the limits.

## @rootsign.trace()

```python theme={null}
@rootsign.trace(
    *,
    tool_name=None,
    redaction_config=None,
    require_approval=False,
    approval_context_builder=None,
    poll_interval_seconds=2.0,
    timeout_seconds=300.0,
    session_context=None,
    ingest_client=None,
)
```

Records every call of the decorated function as an action in the current session: the arguments and the result, each with its hash, and the hash that chains the action to the one before it.

**tool\_name** `str` (default: the function's **name**)

The name recorded for each call.

**redaction\_config** `RedactionConfig`

Rules applied to the arguments and the result before they are hashed and stored. [Redact PII](https://docs.getprovidex.com/guides/redaction.md) covers the built-in configs.

**require\_approval** `bool` (default: False)

Hold each call until a person approves it. Only for plain functions. [Require human approval](https://docs.getprovidex.com/guides/human-approval.md) walks through it.

**approval\_context\_builder** `Callable[[str, dict], dict]`

Builds what the approver is shown, from the tool name and the redacted input. By default: the tool name and the first 500 characters of the redacted input.

**poll\_interval\_seconds** `float` (default: 2.0)

How often a held call checks for a decision, on PostgreSQL.

**timeout\_seconds** `float` (default: 300.0)

How long a held call waits, on PostgreSQL, before it is refused as timed out.

**session\_context** `SessionContext`

Record into this session instead of the current one. Pass it together with `ingest_client`.

**ingest\_client** `IngestClient`

Write through this client instead of the current session's.

**Returns** an async function. A decorated function is always async, even when the original is not, so call it with `await`.

**Raises**, when the decorated function is called:

| Exception | When |
| - | - |
| `RootSignNotInitializedError` | No session is open and no `session_context` was passed |
| `HiTLRejectedError`, `HiTLTimeoutError`, `HiTLUnsupportedBackendError`, `HiTLPersistenceError` | With `require_approval=True`. See [Errors](https://docs.getprovidex.com/reference/errors.md#approval). |
| Whatever the function raises | Unchanged. The call is still recorded, with no result. |

When decorating, `NotImplementedError` is raised for `require_approval=True` on a LangChain or CrewAI tool object.

**Notes**

* Decorating a LangChain `BaseTool` or a CrewAI tool wraps it the same way as `wrap_tools()`.
* A call that cannot be recorded does not fail: RootSign logs a `WARNING` and returns the function's result. The exceptions are held calls, which never run without a record, and the cloud transport with `ROOTSIGN_ON_RECORD_LOSS=fail`, which raises `RecordPersistenceError`.

## rootsign.wrap\_tools()

```python theme={null}
rootsign.wrap_tools(tools, *, ctx=None, client=None, redaction_config=None) -> list
rootsign.wrap_crewai_tools(tools, *, ctx=None, client=None, redaction_config=None) -> list
```

Instruments framework tools so every call is recorded, the same way as `@trace()`. `wrap_tools()` takes LangChain tools, the kind LangGraph's `ToolNode` uses, and needs `pip install 'rootsign[langgraph]'`. `wrap_crewai_tools()` takes CrewAI tools and needs `pip install 'rootsign[crewai]'`.

**tools** `list` *required*

The tools to instrument.

**ctx** `SessionContext`

Record into this session instead of the current one. Pass it together with `client`.

**client** `IngestClient`

Write through this client instead of the current session's.

**redaction\_config** `RedactionConfig`

As for `@trace()`.

```python langchain_tools.py theme={null}
import asyncio

from langchain_core.tools import tool

import rootsign

rootsign.init(agent="support-agent")


@tool
def lookup_customer(customer_id: str) -> str:
    """Look up a customer record."""
    return f"record for {customer_id}"


tools = rootsign.wrap_tools([lookup_customer])


async def main() -> None:
    async with rootsign.session(objective="answer a billing question"):
        print(await tools[0].ainvoke({"customer_id": "acme-corp"}))


asyncio.run(main())
```

```text theme={null}
record for acme-corp
```

**Returns** a list of the same tool objects, instrumented in place, so the originals are wrapped too. Tools that are already instrumented are left as they are, so wrapping twice is harmless.

**Raises** `TypeError` when an item is not a tool of that framework. Nothing in the list is wrapped then:

```text theme={null}
rootsign.wrap_tools() takes LangChain tools (objects with .name, .invoke and .ainvoke); got function 'lookup'. For a plain function, decorate it with @rootsign.trace() instead.
```

**Notes**

* Wrap tools wherever you build them, inside or outside a session. Each call looks up the session it runs in, and a call outside any session raises `RootSignNotInitializedError`.
* `require_approval` is not available for framework tools. Put `@rootsign.trace(require_approval=True)` on the plain function the tool calls.
* The [LangGraph](https://docs.getprovidex.com/integrations/langgraph.md) and [CrewAI](https://docs.getprovidex.com/integrations/crewai.md) pages show complete agents.

## Verify a session

```python theme={null}
rootsign.verify_session_local(jsonl_path) -> VerifyResult
await rootsign.verify_session(session_id, db) -> VerifyResult
```

Recompute every hash in a session and return the verdict. Neither function prints anything or exits.

**jsonl\_path** `str | Path` *required*

A JSONL session file or spool file. `verify_session_local()` only.

**session\_id** `UUID` *required*

A session in the PostgreSQL store. `verify_session()` only.

**db** `AsyncSession` *required*

An open SQLAlchemy async session, for example from `rootsign.database.AsyncSessionLocal()`. `verify_session()` only.

**Returns** a `VerifyResult`: `verdict` (`VALID`, `TAMPERED` or `INCOMPLETE`), `valid`, `record_count`, `first_invalid_sequence`, `missing_ranges`, `error` and `summary`. [Verify a session](https://docs.getprovidex.com/guides/verify.md#verify-from-python) describes each field.

**Raises** `FileNotFoundError` from `verify_session_local()` when the file does not exist.

> [!WARNING]
>
> Neither function reports "nothing to verify" separately. An empty file returns TAMPERED, and an unknown session ID returns VALID with 0 records. Check that the session exists before you rely on the result, or use [`rootsign verify`](https://docs.getprovidex.com/reference/cli-rootsign#rootsign-verify), which exits `3` in these cases.

## Beyond the core calls

`rootsign` also exports the pieces the core calls are built from. They are importable and documented in their docstrings, but they are not part of the stable core, and they can change between releases. [Use RootSign without init()](https://docs.getprovidex.com/guides/without-init.md) shows `register_agent()` and the clients in use:

| Name | What it is |
| - | - |
| `SessionContext` | The object `session()` yields, for building a session yourself |
| `register_agent()` | Registers an agent on PostgreSQL, once, and returns it, for `session(agent_id=...)`. Registering the same name and environment again raises an error that names the existing ID. |
| `get_ingest_client()` | Builds the client for the configured backend |
| `IngestClient` | The base class every client implements, for `session(client=...)` |
| `JsonlIngestClient`, `LocalIngestClient`, `ManagedLocalIngestClient`, `HttpIngestClient`, `BufferedIngestClient` | The clients behind each backend |
| `LangGraphTracer`, `CrewAITracer` | The classes behind `wrap_tools()` and `wrap_crewai_tools()` |
| `RedactionConfig`, `StandardPIIConfig`, `FinancialPIIConfig`, `HealthcarePIIConfig` | Redaction rules. See [Redact PII](https://docs.getprovidex.com/guides/redaction.md). |
| `VerifyResult` | What the verify functions return |
| `AgentEnvironment`, `AgentRiskTier`, `AgentFramework` | The allowed values for `init()`, as enums |

The exceptions are listed in [Errors](https://docs.getprovidex.com/reference/errors.md).

## Related

> [card] [Configuration](https://docs.getprovidex.com/reference/configuration.md)
>
> Every environment variable, and when each is read.

> [card] [Any Python function](https://docs.getprovidex.com/integrations/python.md)
>
> A walkthrough of @rootsign.trace() on plain functions.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.