> For the complete documentation index, see [llms.txt](https://docs.zenml.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zenml.io/kitaru/core-concepts/agents-and-sessions.md). Agents can also search and read this documentation through its [MCP server](https://docs.zenml.io/~gitbook/mcp).

# Agents & Sessions

The agent is the identity, the session is the recording of every model call, tool call, and decision a run left behind.

Most traces are transcripts: you read them. A Kitaru **session** is a recording you can run. It holds everything one agent run did, including every model call, tool call, and decision, in order, with inputs and outputs. That is what [replay](https://docs.zenml.io/kitaru/core-concepts/replay.md) needs to re-execute the run against your real code.

Two nouns carry the whole data model:

* An **agent** is the stable identity your runs attach to. You register it once, and every session, cohort, and experiment references it.
* A **session** is one recorded run of that agent. Sessions arrive three ways: recorded live by an adapter, [imported](https://docs.zenml.io/kitaru/import-your-traces/import-your-traces.md) from your existing traces, or produced by a replay. All three are the same object with a different `origin`: `recorded`, `imported`, or `replay`.

## Agents and agent versions

Register an agent with the CLI:

```bash
kitaru agent register support-agent \
  --command "python support.py" \
  --description "Resolves support tickets"
```

This creates the agent and its first **agent version** in one step. An agent version stores an optional `RunSpec` pointer (the command that starts your agent, its working directory, environment, secrets, and timeout) plus optional capability metadata (tools, MCP servers, skills). Omit `--command` when registering an agent that only receives imported traces; attempting a replay or experiment against a version with no run spec returns an actionable error until you register or update a version with `--command`. The run spec is what a [worker](https://docs.zenml.io/kitaru/core-concepts/workers.md) executes when a replay or experiment re-runs the agent in your environment.

{% hint style="warning" %}
An agent version does **not** snapshot your source code or pin a git commit hash. If you edit files in place inside the same working directory, older agent versions pointing at that directory run the mutated code rather than the code they were registered with. For reproducible baselines, point each version at its own directory, git worktree, or container image, and attach a `--display-version` label (such as a git SHA or tag).
{% endhint %}

To register from a YAML or JSON spec, put the agent's description under `parent` and the version's description under `version`:

```yaml
parent:
  description: Resolves support tickets
version:
  description: Adds refund lookup
  run_spec:
    command: python support.py
```

```bash
kitaru agent register support-agent --spec agent.yaml
```

A top-level `description` is also accepted as shorthand for `parent.description`. If you provide both, they must agree. `version.description` always describes only that version. A registration spec supplies the complete definition, so do not combine `--spec` with direct options such as `--description` or `--command`. For `agent version register --spec`, the document contains only the version fields, without the `parent` and `version` sections.

Versions are server-numbered (1, 2, 3, …); `--display-version` attaches your own label, such as a semver, a git SHA, or a branch name:

```bash
kitaru agent version register support-agent \
  --command "python support.py" \
  --display-version "pr-1284"
```

Register a new version when the code changes. An [experiment](https://docs.zenml.io/kitaru/core-concepts/experiments.md) is precisely "replay this cohort on that agent version and see what moved." When you need to fix a moved `--working-dir`, environment variable, or `runtime_capabilities` on an existing version without creating a new version number, use `kitaru agent version update support-agent@1 ...` (updating in place changes what recorded replays and experiments run against).

Credentials the agent needs at run time, such as a model provider key, go into a [secret](https://docs.zenml.io/kitaru/running-in-production/secrets.md) rather than `--env`. Reference the secret in the version, and the worker injects each of its keys as an environment variable when it runs the agent:

```bash
kitaru agent version register support-agent \
  --command "python support.py" \
  --secret-id <openai-secret-id>
```

In a spec document the same reference is `run_spec.secret_ids`.

## Runtime capabilities

The run spec also declares what the runtime can do during a re-run. `runtime_capabilities` holds two booleans, both `true` by default: `overrides`, whether the runtime can apply [replay](https://docs.zenml.io/kitaru/core-concepts/replay.md) overrides (model, prompts, model params), and `tool_policies`, whether it can apply non-passthrough [tool policies](https://docs.zenml.io/kitaru/guides/tool-policies.md). Both work by intercepting model and tool calls inside the agent process. Some runtimes execute the agent for real and cannot intercept anything, and the server cannot tell that from the run command, so the version declares it.

Recording adapters intercept calls and keep the defaults. Declare both `false` when the version runs an importer-backed adapter, which records by importing the provider trace after the run and never intercepts a call. Set the declaration in the run spec at registration, via a spec document:

```yaml
run_spec:
  command: python agent.py
  runtime_capabilities:
    overrides: false
    tool_policies: false
```

```bash
kitaru agent version register support-agent --spec spec.yaml
```

Creating a replay or starting an experiment run is rejected with 422 when its config carries an override or a non-passthrough tool policy the version's declared capabilities cannot apply. Runtimes that cannot apply them also fail the run when such a config reaches them anyway.

## What a session records

A session carries its top-level `inputs`, `outputs`, `status` (`in_progress` / `completed` / `failed`), timing, and rolled-up totals: `cost`, `tokens` (input / output / cached / reasoning), `llm_call_count`, and `tool_call_count`.

The step-by-step recording lives in the session's **nodes**: an ordered tree with one node per event.

| Node type       | What it records                                                                                         |
| --------------- | ------------------------------------------------------------------------------------------------------- |
| `llm_call`      | Requested and resolved model, inputs and outputs, token usage, cost, model params                       |
| `tool_call`     | Tool name, arguments, result, plus the cache key replay uses to answer the same call from the recording |
| `subagent_call` | A delegated run by a sub-agent                                                                          |
| `span`          | Any other grouping the adapter or importer wants to preserve                                            |

Adapters record nodes automatically. The [PydanticAI adapter](https://docs.zenml.io/kitaru/adapters/pydantic-ai.md) batches them to the server as the run progresses; importers write the same structure from your existing traces. There is one shape, so replay and evaluators never care where a session came from.

## One session is one end-to-end run

This is the most important thing to get right when you bring your own traces, and the easiest to get wrong.

A session is **the whole run, from the request that started it to the answer that ended it**, including every model call, tool call and sub-agent hop in between. It is not one model call, and it is not one span. Replay re-executes a session from the top, so a session that holds half a run can only ever reproduce half a run, and a cohort of them measures nothing you care about.

Adapters get this for free: the wrapper opens the session when your agent is invoked and closes it when the call returns. Importing needs a decision from you, because observability tools do not agree on what a trace is:

* Some emit **one trace per run**, which maps to one session directly. Nothing to do.
* Many emit **one trace per conversation turn**, so a five-turn support conversation arrives as five traces. If that is one run in your product, those five traces are one session.
* Some emit **one trace per model call**, which almost never matches a session on its own.

You do not have to reshape the export yourself. Importers group related traces into one session using the provider's own conversation or session identifier, and `--join-on` names the field to group on when the identity lives somewhere else. See [Join provider traces into sessions](https://docs.zenml.io/kitaru/import-your-traces/importing-sessions.md). When no identifier is present, each trace becomes its own session, which is the safe default but rarely the one you want for multi-turn agents.

So the question before importing is not "what does my tool call a trace" but **"what does my product call one run"**. Then make the import produce that. If the answer is "it depends on how we configured tracing", resolve that upstream if you can: consistent session identity in your traces is what makes cohorts, experiments, and regression suites mean the same thing every time.

If you are joining a format no importer understands, do the joining in your [custom importer](https://docs.zenml.io/kitaru/import-your-traces/importing-sessions.md) rather than after the fact. Sessions are not merged once they land.

## Reading sessions back

The Python client is async; every resource follows the same `list` / `iter` / `get` pattern:

```python
import asyncio
from kitaru.client import KitaruAPIClient
from kitaru.api_models.v1.session import SessionListParams
from kitaru.api_models.v1.session_node import SessionNodeListParams


async def main() -> None:
    client = KitaruAPIClient()  # KITARU_API_URL, KITARU_API_KEY
    page = await client.sessions.list(SessionListParams())
    for session in page.items:
        print(session.id, session.origin, session.status, session.cost)

    nodes = await client.sessions.list_nodes(
        page.items[0].id, SessionNodeListParams(include_payloads=True)
    )
    for node in nodes.items:
        print(node.index, node.node_type, node.name)


asyncio.run(main())
```

Node payloads (inputs, outputs) are returned only when you ask (`include_payloads=True`); listings stay cheap by default. The CLI mirrors both reads:

```bash
kitaru session list --agent support-agent --origin recorded
kitaru session nodes <session-id> --include-payloads
```

`session list` returns one page, with 20 sessions by default. Use `--size` (or its alias `--limit`) to request 1–1,000 sessions per page:

```bash
kitaru session list --agent support-agent --tag imported-baseline --size 100 -o json
```

The JSON response includes `items` and `page.next_cursor`. If that cursor is not null, pass it with `--cursor` and keep the same filters and sort to read the next page. Stop when `page.next_cursor` is null. `--limit` sets the page size; it does not fetch every page or cap a separate all-results operation. Use either `--size` or `--limit` in one invocation.

To read a whole session at once, `kitaru session read <session-id>` prints a compact transcript: the request and system prompt once, then one line per node with each model call's new text, short tool arguments and results, and errors in full. Long fields end in `[+N chars]`; `kitaru session read <session-id> --node 4 --full` shows the node at position 4 without cuts.

Sessions attach to the rest of the system by reference: a [cohort version](https://docs.zenml.io/kitaru/core-concepts/cohorts.md) pins a set of session ids, an [evaluation](https://docs.zenml.io/kitaru/core-concepts/evaluators.md) row evaluates one session, and a [replay](https://docs.zenml.io/kitaru/core-concepts/replay.md) points at its baseline session and produces a result session. **Tags** group resources ad hoc before they graduate into a cohort or another durable structure. A tag can link to a session, cohort, cohort version, agent version, experiment, or experiment run. Apply one to a whole import with `kitaru session import --tag ...`, then select on it anywhere that resource supports a `tag` filter, such as `kitaru session evaluate --tag ...`.

In the Python client, filter sessions by the tag's name with `field="tag"`. The session filter also accepts `field="tags"` as an alias. `eq` matches one tag name; `in` matches any of the supplied names. To require several tags, combine separate conditions with `and`.

```python
import asyncio

from kitaru.api_models.v1.filter import FilterCondition, FilterOp
from kitaru.api_models.v1.session import SessionListParams
from kitaru.client import KitaruAPIClient


async def main() -> None:
    params = SessionListParams(
        size=100,
        filter=FilterCondition(field="tag", op=FilterOp.EQ, value="imported-baseline"),
    )
    async with KitaruAPIClient() as client:
        async for session in client.sessions.iter(params):
            print(session.id, [tag.name for tag in session.tags])


asyncio.run(main())
```

`list()` returns one page, while `iter()` follows cursors until all matching sessions have been read. Session list and detail responses include the currently linked tags; sessions without tags return an empty list. The Python client also accepts older server responses without the `tags` field, using an empty list, so an empty list from an older server does not establish that the session has no tags.

The native MCP server can list and filter tags, use existing filtered reads to rediscover tagged resources, and create, rename, link, unlink, or delete tags according to its capability mode. It cannot enumerate every link belonging to a tag. Deleting a tag removes all of its resource links; it does not delete the linked resources.

## Deleting agents and sessions

Deleting an agent with `kitaru agent delete <agent> --force` is a **soft delete**. It hides the agent from `kitaru agent list` and blocks new sessions, versions, cohorts, or runs under that agent, while keeping all existing sessions, versions, cohorts, investigations, and imports stored under the old `agent_id`. Soft-deleted agents cannot be restored. If you register a new agent with the same name later, it receives a new `agent_id`, so name-based filters such as `--agent support-agent` point at the new agent rather than the old data.

The `agent delete` output prints the deleted agent's `id` along with bounded counts of retained sessions and cohorts. Use that old `agent_id` to inspect or remove retained sessions:

```bash
kitaru session list --filter '{"field":"agent_id","op":"eq","value":"<old-agent-id>"}'
kitaru session delete --filter '{"field":"agent_id","op":"eq","value":"<old-agent-id>"}' --force
```

`kitaru session delete` also accepts explicit session UUIDs or `--sessions-file` alongside `--force`. A session that is still referenced by a cohort version, an investigation, or a replay cannot be deleted until that reference is removed; the server returns a 409 conflict naming what still references it.

## Where sessions come from

* **Recorded:** wrap your agent with an adapter and run it as usual. See the [adapter overview](https://docs.zenml.io/kitaru/adapters/adapters.md).
* **Imported:** bring the traces you already collect. Langfuse stays your system of record; Kitaru gets a runnable copy. See [Import your traces](https://docs.zenml.io/kitaru/import-your-traces/import-your-traces.md).
* **Replay:** every replay produces a new session with `origin: replay`, evaluated by the same evaluators as any other session. See [Replay](https://docs.zenml.io/kitaru/core-concepts/replay.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.zenml.io/kitaru/core-concepts/agents-and-sessions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
