> 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/sdk-reference/kitaru/example-usages.md).

# Python examples

Common patterns with the Kitaru Python SDK. For complete signatures, see the [Python SDK reference](https://sdkdocs.kitaru.ai/reference/python/). For the TypeScript client, see [How to use the SDK](https://docs.zenml.io/kitaru/get-help/sdks). For supported frameworks, see the [adapter overview](https://docs.zenml.io/kitaru/adapters/adapters).

## Record an agent's runs

Wrap an existing PydanticAI agent and every run is recorded as a session: each model call and tool call becomes a node. Each adapter ships as its own distribution, installed alongside `kitaru`. Here, install it with `uv add kitaru-pydantic-ai`:

```python
import os
import uuid

from pydantic_ai import Agent
from kitaru_pydantic_ai import KitaruAgent

base_agent = Agent("openai:gpt-5.4", system_prompt="You resolve support tickets.")

agent = KitaruAgent(base_agent, agent_id=uuid.UUID(os.environ["KITARU_AGENT_ID"]))
result = agent.run_sync("Refund order #4821; the card reader was double-charged.")
```

## Inspect sessions

```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:
    async with KitaruAPIClient() as client:
        sessions = await client.sessions.list(SessionListParams(size=5))
        if not sessions.items:
            return
        baseline_session_id = sessions.items[0].id
        nodes = await client.sessions.list_nodes(
            baseline_session_id, SessionNodeListParams(include_payloads=True)
        )
        print(baseline_session_id, len(nodes.items))


asyncio.run(main())
```

## Replay with one thing changed

Answer tool calls from the recorded session, swap the model, and run the same evaluator on both sides:

```python
import asyncio
import os
import uuid

from kitaru.client import KitaruAPIClient
from kitaru.api_models.v1.replay import ReplayCreateRequest
from kitaru.api_models.v1.replay_config import (
    EvaluatorConfig,
    HistoryConfig,
    ReplayOverride,
    ToolPolicy,
)


async def main() -> None:
    baseline_session_id = uuid.UUID(os.environ["KITARU_BASELINE_SESSION_ID"])
    async with KitaruAPIClient() as client:
        replay = await client.replays.create(
            ReplayCreateRequest(
                baseline_session_id=baseline_session_id,
                override=ReplayOverride(
                    model={"openai:gpt-5.4": "openai:gpt-5-nano"}
                ),
                evaluators=[EvaluatorConfig(evaluator="refund-check")],
                tool_policy=ToolPolicy(
                    default=HistoryConfig(scope="baseline", on_miss="fail")
                ),
                evaluate_baselines=True,
            )
        )
        print(replay.id, replay.job_id)


asyncio.run(main())
```

The full walkthrough, from recording through replaying cohorts and experiments, is the [Kitaru quickstart](https://docs.zenml.io/kitaru/getting-started/quickstart).


---

# 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/sdk-reference/kitaru/example-usages.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.
