Skip to main content
Working with an agent? Give them a link to this page as markdown.

Python SDK

Reference for the client library in the Watcher SDK: typed sync and async Python clients for the Watcher API, with automatic retry, pagination, and structured error handling.

The SDK integration examples show how to connect agents, frameworks, gateways and observability tools.

Library users can install the optional cli extra for watcher-sdk auth login|status|logout.

Installation​

The SDK is available as a Python wheel attached to each Watcher client release on GitHub. Releases with a watcher_sdk-<version>-source.tar.gz asset also include MIT-licensed SDK source under sdk/ in the release's Git tag. The source tree also includes sdk/openapi.json, the OpenAPI spec the generated client was built from. Wheels remain available. Both formats require Python 3.12 or later and provide the same SDK code. Both include py.typed markers, so type checkers such as mypy and Pyright check your calls against the SDK's type annotations.

Choose the release matching your server version. Replace <version> in the commands below with that version, without the leading v.

Install the wheel​

curl -fsSLO https://github.com/ApolloResearch/watcher-bin/releases/download/v<version>/watcher_sdk-<version>-py3-none-any.whl
pip install ./watcher_sdk-<version>-py3-none-any.whl

The package is not on PyPI. Each release's checksums.txt includes the wheel's SHA-256 digest.

Install source​

Check that the selected release includes watcher_sdk-<version>-source.tar.gz. If it does not, install its wheel instead. For a release with source, install from the version tag with Git available:

pip install 'watcher-sdk @ git+https://github.com/ApolloResearch/watcher-bin.git@v<version>#subdirectory=sdk'

For a uv-managed project:

uv add 'watcher-sdk @ git+https://github.com/ApolloResearch/watcher-bin.git@v<version>#subdirectory=sdk'

To install without Git, download the source archive instead:

curl -fsSLO https://github.com/ApolloResearch/watcher-bin/releases/download/v<version>/watcher_sdk-<version>-source.tar.gz
pip install ./watcher_sdk-<version>-source.tar.gz

The source archive's SHA-256 digest is in the release's checksums.txt. The source export also contains integration examples under sdk/examples/. Extract the archive with tar -xzf watcher_sdk-<version>-source.tar.gz and keep the whole sdk/ directory together to use them. Each example has its own installation instructions and tests; installing the SDK does not install the examples. Older source releases may not contain examples.

Source installation builds the Python package locally, so an offline install also needs cached build and runtime dependencies. The generated client is included; no API schema generation is needed.

Prerelease source is available by tag before production promotion. When a release includes source, production promotion moves the repository's main branch to that release's tag. Promoting a release without source leaves main unchanged, so it can differ from the current production version. Pin a version tag or commit for reproducible installations. Third-party dependencies retain their own licenses; the SDK includes their license notices.

Quick Start​

These examples authenticate with an organization API key, which exists on SSO-mode deployments. On a proxy-mode deployment there are no Watcher API keys; see Proxy-mode deployments below.

Synchronous​

import os

from watcher_sdk import WatcherClient

with WatcherClient(
"https://watcher.example.com/api",
api_key=os.environ["WATCHER_API_KEY"],
) as client:
# List all sessions
sessions = client.list_sessions()
print(f"Total sessions: {sessions.total}")

# Iterate over all sessions (auto-paginates)
for session in client.list_sessions_iter():
print(session.session_id, session.trajectory_count)

# Get available graders
graders = client.list_graders()
for g in graders.graders:
print(g.name, list(g.scores.additional_keys))

Asynchronous​

import os

from watcher_sdk import AsyncWatcherClient

async with AsyncWatcherClient(
"https://watcher.example.com/api",
api_key=os.environ["WATCHER_API_KEY"],
) as client:
sessions = await client.list_sessions()

async for session in client.list_sessions_iter():
print(session.session_id)

Configuration​

Both WatcherClient and AsyncWatcherClient accept the same constructor parameters:

ParameterTypeDefaultDescription
base_urlstr"http://localhost:8000"Watcher API base URL
api_keystr | NoneNoneOrganization API key, sent via x-api-key header
access_tokenstr | NoneNoneJWT access token from the sign-in flow, sent via Authorization: Bearer header
timeoutfloat30.0Request timeout in seconds
max_retriesint3Max retries for transient failures
httpx_clienthttpx.AsyncClient | NoneNonePre-configured HTTP client, for auth the other parameters cannot express (see below)
manage_external_httpx_client_lifecycleboolTrueWhether the SDK closes a provided httpx_client on exit

For a deployed Watcher, base_url is the API base URL including its /api prefix, e.g. https://watcher.example.com/api; the SDK appends the /v1/... endpoint paths itself. The localhost default suits a local development server.

The constructors accept two explicit credential types:

  • api_key -- sends the x-api-key header. Use with organization API keys, created on the Analyzer's Organization page (the OpenAPI spec calls them WorkOS API keys).
  • access_token -- sends the Authorization: Bearer header. Use with the JWT access token from a user's sign-in.

If both are provided, access_token takes precedence.

# Using an organization API key
client = WatcherClient(
base_url="https://watcher.example.com/api",
api_key="my-org-api-key",
timeout=60.0,
max_retries=5,
)

# Using a JWT access token
client = WatcherClient(
base_url="https://watcher.example.com/api",
access_token="eyJhbG...",
)

Sign in without a Watcher client​

Local integrations on macOS and Linux can sign in through the browser. This works with Watcher Cloud and self-hosted WorkOS SSO. No local Watcher client is required.

With the optional cli extra installed, sign in from a terminal:

watcher-sdk auth login --url https://watcher.example.com # self-hosted
watcher-sdk auth login --cloud # Watcher Cloud
watcher-sdk auth status
watcher-sdk auth logout --url https://watcher.example.com

login with neither option signs in to Watcher Cloud. status lists saved SDK and Watcher sign-ins and checks access to each server. --state-dir selects another SDK credential directory. The Python functions below do the same from code, and discover_logins loads a sign-in that the commands saved.

import asyncio
import webbrowser
from pathlib import Path
from watcher_sdk import DeviceLoginPrompt, login


def open_login(prompt: DeviceLoginPrompt) -> None:
webbrowser.open(prompt.url)


async def main():
auth = await login("https://watcher.example.com/api", open_login)
profile = Path.home() / ".config/watcher-sdk/integration.json"
profile.parent.mkdir(parents=True, exist_ok=True)
profile.write_text(auth.model_dump_json(), encoding="utf-8")
async with auth.create_client() as client:
sessions = await client.list_sessions(limit=10)


asyncio.run(main())

login checks the approval link and code before it calls your function. It raises ValueError if the link is not HTTPS, has no host, contains a user name or password, has a # fragment or is longer than 2048 characters. It also raises ValueError if the code is empty or longer than 32 characters.

Call login only from interactive setup. Save the returned IntegrationAuth reference for later use; its JSON contains the server and credential location, not tokens. create_client reads and refreshes the saved sign-in without a browser. Later processes load the same reference:

from pathlib import Path
from watcher_sdk import IntegrationAuth

profile = Path.home() / ".config/watcher-sdk/integration.json"
auth = IntegrationAuth.model_validate_json(profile.read_text(encoding="utf-8"))

Use auth.create_client() in an async context, as in the setup example.

discover_logins lists usable saved SDK and Watcher sign-ins, skipping unreadable or invalid files. It reads local files only, without a network call, and returns a list of IntegrationAuth references whose kind is sdk_login or watcher_login. Pass base_url, with or without the /api suffix, to check one server; an invalid matching sign-in then raises an error. The caller must select a sign-in when more than one is available. For example, to use the sign-in that watcher-sdk auth login saved:

import asyncio
from watcher_sdk import discover_logins


async def main():
logins = discover_logins("https://watcher.example.com")
auth = next(login for login in logins if login.kind == "sdk_login")
async with auth.create_client() as client:
sessions = await client.list_sessions(limit=10)


asyncio.run(main())

SDK sign-ins live under ~/.config/watcher-sdk, or $XDG_CONFIG_HOME/watcher-sdk. WATCHER_SDK_HOME or an explicit state_dir selects another SDK store. Each server has a separate credential file. logout asks the server to end the session that the SDK sign-in holds, so a copied refresh token stops working. Then it removes the SDK's local sign-in. It leaves the Watcher client's saved sign-in file alone. The server may end more than the SDK's sign-in, though: the browser that approved the sign-in may be signed out of Watcher too, and so may a Watcher client that signed in through that browser. If the server cannot be reached, logout still removes the local sign-in, and the server session lasts until it expires. An expired or rejected sign-in never falls back to another identity.

login creates the credential folder so that only your account can open it. If that folder already exists, login restricts it the same way. It refuses a credential folder that is a symbolic link, so it never changes the permissions of another file or folder. The SDK refuses a saved sign-in when another account owns it or can write to it. That account could replace the file with its own sign-in, and your sessions would upload to its account. Run login again to replace a refused sign-in.

verify_integration_auth checks ingestion and session read access without uploading a test session. User sign-ins need access to their own sessions; API keys also need sessions:findings:read:any. Successful setup does not prove a coding agent has loaded its hooks or delivered a session.

Reuse a saved Watcher sign-in​

Local integrations on macOS and Linux can reuse the installed Watcher client's sign-in. Sign in to Watcher for the target server, then run the integration as the same OS user:

import asyncio
from watcher_sdk import async_client_from_watcher


async def main():
async with async_client_from_watcher("https://watcher.example.com/api") as client:
profile = await client.auth_me()
sessions = await client.list_sessions(limit=10)


asyncio.run(main())

The helper reads current tokens from Watcher's credential store on each request. It shares Watcher's file lock when refreshing a rejected token, then retries once. It requires a saved sign-in for the exact server URL, including its port and path. It never opens a sign-in page or falls back to another sign-in or API key. If the sign-in is missing or its refresh token is rejected, sign in to Watcher again. Also sign in again if Watcher answers a refresh with a redirect: the SDK never follows it, and saves nothing.

The default store is ~/.apollo_monitor. Set APOLLO_MONITOR_HOME or pass state_dir=Path("/path/to/watcher-state") if Watcher uses another location. The helper works with the packaged app and needs neither a running Watcher daemon nor its Python package. Install the SDK in the integration's environment. Use explicit service credentials for remote gateways and unattended services.

For a custom HTTP client, use WatcherLoginAuth(base_url) as the auth on an httpx.AsyncClient, then pass that HTTP client through httpx_client to either SDK client. Token refreshes never go through your client. The SDK sends each one on its own connection, which never follows a redirect or keeps a cookie. Your client's cookie jar, hooks and redirect setting never see the refresh token. Supplying access_token alone does not enable saved sign-in discovery or refresh.

import httpx
from watcher_sdk import AsyncWatcherClient, WatcherLoginAuth

url = "https://watcher.example.com/api"
http_client = httpx.AsyncClient(base_url=url, auth=WatcherLoginAuth(url))
client = AsyncWatcherClient(base_url=url, httpx_client=http_client)

Your client's connection settings do not apply to refreshes either. Refreshes ignore the verify, cert, proxy, trust_env and transport options you set on it. By default, the refresh connection reads proxy and certificate settings from the environment, such as HTTPS_PROXY, HTTP_PROXY and SSL_CERT_FILE. It reads them even when your client sets trust_env=False.

If you set up TLS, client certificates or a proxy in code, build one httpx.AsyncHTTPTransport. Pass it to your client and to WatcherLoginAuth(base_url, transport=...), and refreshes go through it too. The SDK hands each refresh to that transport directly, so redirects and cookie jars still never apply. The SDK never closes the transport.

import ssl
import httpx
from watcher_sdk import AsyncWatcherClient, WatcherLoginAuth

url = "https://watcher.example.com/api"
tls = ssl.create_default_context(cafile="/etc/ssl/certs/watcher-ca.pem")
transport = httpx.AsyncHTTPTransport(verify=tls)
http_client = httpx.AsyncClient(base_url=url, auth=WatcherLoginAuth(url, transport=transport), transport=transport)
client = AsyncWatcherClient(base_url=url, httpx_client=http_client)

Proxy-mode deployments​

Proxy-mode deployments have no Watcher API keys: requests must satisfy your reverse proxy instead. Pass a pre-configured httpx.AsyncClient carrying whatever the proxy accepts (both WatcherClient and AsyncWatcherClient take an httpx.AsyncClient; the sync client drives async internally):

import httpx
from watcher_sdk import WatcherClient

http = httpx.AsyncClient(
base_url="https://watcher.example.com/api",
auth=("service-account", "password"), # e.g. basic auth
# or headers={"CF-Access-Client-Id": "...",
# "CF-Access-Client-Secret": "..."},
timeout=30.0,
)

with WatcherClient(httpx_client=http) as client:
...

The provided client replaces the SDK-managed one entirely, so set base_url, auth, and timeout on it; the constructor's base_url, api_key, access_token, and timeout parameters do not apply to it. Retries, pagination, and error mapping still work as usual.

The remaining examples on this page abbreviate construction as WatcherClient(); against a real deployment, pass base_url and api_key as above (or httpx_client in proxy mode).

Both clients must be used as context managers (or opened/closed manually) to manage HTTP connections:

# Context manager (recommended)
with WatcherClient() as client:
...

# Manual lifecycle
client = WatcherClient()
client.open()
try:
...
finally:
client.close()

Ingest​

For bounded Claude Code and Codex source uploads, see Incremental ingest for the HTTP contract and resumable upload guidance. The examples below use ingest to send full trajectories.

Ingest trajectories for storage and deduplication:

from watcher_sdk import WatcherClient, ClaudeCodeTrajectory

with WatcherClient() as client:
response = client.ingest(
source="my-app",
trajectories=[trajectory], # list of ClaudeCodeTrajectory objects
)
print(f"Session: {response.session_id}")
print(f"Trajectory IDs: {response.trajectory_ids}")
print(f"Ingested: {response.ingested_count}, Duplicates: {response.duplicate_count}")

Each trajectory is a ClaudeCodeTrajectory (a Claude Code session file's entries), a CodexTrajectory (a Codex rollout's entries), or a CopilotTrajectory (one conversation's events from a GitHub Copilot CLI events.jsonl, with agent_id set to None for the main conversation or to a subagent's ID).

For a ClaudeCodeTrajectory, set session_id to the current Claude Code conversation ID and keep it the same on full uploads and deltas. It overrides historical sessionId values in copied entries. If omitted, each parsed branch uses its first entry's sessionId. Ownership checks still apply.

Common IngestResponse fields:

  • session_id: UUID of a session containing returned trajectories (a batch can span sessions)
  • trajectory_ids: list of leaf trajectory hashes (unique identifiers)
  • ingested_count: number of newly stored leaf trajectories
  • duplicate_count: number of already-stored leaf trajectories recognized
  • skipped_count: number of uploads skipped (e.g., empty entries)
  • append_anchor: {session_id, trajectory_id} for the next append, or null

One upload can produce several trajectory IDs and counts: a rewound Claude Code conversation stores one trajectory per branch. Use the explicit append_anchor pair to continue the final nonempty input's live branch. The returned trajectory IDs remain available for grading and queries.

When append_anchor is null, do not advance the append cursor. A partial upload may end inside a longer stored chain; upload a larger snapshot to recover an anchor. Only older servers that omit append_anchor use session_id and the last trajectory_ids entry as the append anchor.

Child sessions​

For Claude Code, set ClaudeCodeTrajectory.agent to ClaudeCodeAgentIdentity(root_session_id=..., agent_id=...) on every full and append upload. Upload each agent separately to keep its transcript in its own session.

Each entry's sessionId and agentId, when present, must match the declared identity. Custom imports may omit agentId on records with a message or attachment if they set isSidechain: true. Metadata records without either payload may omit both markers. Conflicting IDs or content without a child marker return HTTP 422.

For Codex, upload each child's own rollout with CodexTrajectory.

Once both sessions exist, call client.put_agent_relationship with the child's returned session ID and an AgentRelationshipRequest(parent_session_id=...). Use the immediate parent's returned server session UUID as parent_session_id. Spawning-call details are optional. Supply spawning_tool_use_id to have the server find that call in the parent's uploaded messages. A successful response with spawning_message_trajectory_hash: null means the message is not yet stored; retry after ordinary uploads finish. Linking does not reupload the parent. If stored representations repeat the call, the first inserted match is retained. Both sessions must be writable under the caller's ingestion permissions. Established parentage and resolved message links cannot change. The models are exported from watcher_sdk.

Grade​

Grade trajectories using configured LLM-based graders. Check the grading permissions for stored sessions and caller-selected graders before running the example:

with WatcherClient() as client:
# Grade specific trajectories. grader_ids is required: name every grader
# to run (list them with client.list_graders()).
response = client.grade(
session_id="your-session-uuid",
trajectory_ids=["trajectory-hash-1", "trajectory-hash-2"],
grader_ids=["grader-uuid-1"],
)

for grade in response.grades:
print(f"Trajectory: {grade.trajectory_hash}")
print(f"Grader: {grade.grader_id}")
print(f"Scores: {grade.grades.to_dict()}")

GradeResponse fields:

  • grades: list of GradeResult objects
  • not_found_ids: trajectory IDs that were not found
  • cost_usd: estimated request cost for role grading; null for grader-UUID grading

GradeResult fields:

  • trajectory_hash, grader_id, session_id, model: identifiers (model is null for grades produced through monitors, where the model is a server-side choice)
  • grades: score values (dict-like object)
  • created_at: when the grade was produced
  • error: error message if grading failed for this trajectory

Grading with Monitors​

Watcher-managed monitors are addressed by role instead of grader UUID. The server resolves everything about how the grade is produced:

with WatcherClient() as client:
monitors = client.list_monitors() # the monitors this server runs
response = client.grade_by_role(
"triage",
messages=[[{"role": "user", "content": "..."}]],
)

list_monitors raises WatcherNotFoundError on servers without managed monitors; grade by grader UUID there instead.

The role must be triage or gateway; other roles return HTTP 422. Send exactly one of three payloads:

  • input: one pending tool call as data. Fields are tool_name (required), tool_input, messages (the conversation so far), cwd and prior_decisions. A grading_guidelines field is accepted but ignored: to add policy, use custom instructions. Watcher applies the monitor's context policy, as for gateway hooks, and shortens a transcript too long for the monitor's model by cutting its middle. Use this mode to grade a pending call.
  • messages: complete prompt message lists, graded exactly as sent. They are not shortened, so one too long for the monitor's model fails.
  • trajectory_ids with session_id: stored trajectories.
with WatcherClient() as client:
response = client.grade_by_role(
"gateway",
input={
"tool_name": "Bash",
"tool_input": {"command": "rm -rf build/"},
"cwd": "/home/dev/project",
"messages": [{"role": "user", "content": "Clean the build output."}],
},
)
print(response.grades[0].grades.to_dict()) # includes decision_score

A triage grade carries only decision_score. With input or messages, you can also send session_id and tool_use_id to record the grade against that call.

grade_by_role calls POST /v1/monitors/{role}/grade. Its response includes cost_usd, the estimated USD cost across this request's trajectories and retries. Only the selected role runs. A supplied triage assessment adds no cost. The estimate uses the prices set on the server. It is null when usage or pricing is missing. For stored-session grading, it is also null if you cannot read that session's findings or content. Otherwise, it is 0 when no model runs. Older servers may omit the field. Provider retries whose usage is not reported cannot be included.

Ingest and Grade (Convenience)​

Ingest and immediately grade in one call:

with WatcherClient() as client:
ingest_response, grade_response = client.ingest_and_grade(
source="my-app",
trajectories=[trajectory],
grader_ids=["grader-uuid-1"], # required: the graders to run
)

List Graders​

List available graders and their score definitions:

with WatcherClient() as client:
response = client.list_graders()
for grader in response.graders:
print(f"{grader.name} ({grader.grader_id})")
print(f" Description: {grader.description}")
for score_name, score_def in grader.scores.additional_properties.items():
print(f" {score_name}: {score_def.type_} [{score_def.min_value}-{score_def.max_value}]")

GraderSummary fields:

  • grader_id: UUID of the grader
  • name: human-readable name
  • description: what the grader evaluates
  • scores: mapping of score names to ScoreDefinition (type_, description, min_value/max_value)

Sessions​

List Sessions​

List sessions with pagination and sorting:

with WatcherClient() as client:
response = client.list_sessions(
limit=50, # max results per page (1-1000, default 100)
offset=0, # pagination offset
sort_by="ended_at", # or "started_at"
sort_order="desc", # or "asc"
)
print(f"Showing {len(response.sessions)} of {response.total} sessions")
for session in response.sessions:
print(f" {session.session_id}: {session.trajectory_count} trajectories")

Search Sessions​

Search with filters (date range, external IDs):

from datetime import datetime, timezone

with WatcherClient() as client:
response = client.search_sessions(
start_date=datetime(2026, 1, 1, tzinfo=timezone.utc),
end_date=datetime(2026, 2, 1, tzinfo=timezone.utc),
external_ids={"anthropic-claude-code": ["session-abc-123"]},
limit=100,
)

The external_ids parameter filters by source-specific identifiers. Keys are namespace strings (e.g., "anthropic-claude-code" for sessions recorded from Claude Code), values are lists of IDs to match.

start_date and end_date select sessions whose activity overlaps the window: sessions with activity at or after start_date that started at or before end_date. A session's recorded span runs from its first through its latest message, so a session still in progress matches if it has had activity at or after start_date. Sessions with no recorded message activity never match. Scrubbed sessions retain their recorded activity and can still match.

To include or exclude session owners, pass a UserFilter to search_sessions or search_sessions_iter. Both WatcherClient and AsyncWatcherClient accept the same filter:

from uuid import UUID
from watcher_sdk import UserFilter, UserFilterMode, WatcherClient

users = UserFilter(
ids=[
UUID("aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"),
UUID("bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb"),
],
mode=UserFilterMode.EXCLUDE,
)

with WatcherClient() as client:
response = client.search_sessions(users=users, limit=100)
for session in client.search_sessions_iter(users=users, page_size=100):
print(session.session_id)

ids must contain at least one user UUID, and mode is required. UserFilterMode.INCLUDE matches any listed owner; UserFilterMode.EXCLUDE removes all listed owners while keeping unattributed sessions. Omit users to search all sessions you are allowed to read. Both filter modes follow the findings access rules.

Filter by coding agent​

agent_platform is an open string in session responses. Clients must accept additional values. Watcher can recognize retained SDK adapter IDs, including on older sessions whose stored platform is other.

Use agent_platforms=["github_copilot_cli"] to find Copilot sessions. agent_platforms=["other"] excludes recognized agents. Multiple names select any of those agents; all other filters still apply. Counts and pagination use the same rules. Missing, unknown or conflicting IDs remain other.

The coding agent and its chosen model are separate. Recognition needs no adapter update, hook reinstall or new sign-in. It does not change recording, ownership or tool rules. See integration limits for the monitoring features each adapter provides.

Filter by ingestion source​

Pass typed capture_sources to search_sessions or search_sessions_iter to select integrations that delivered a session. Sync and async clients accept this filter:

from watcher_sdk.capture import CaptureSource

with WatcherClient() as client:
for session in client.search_sessions_iter(
capture_sources=[CaptureSource(type="gateway", name="litellm")],
agent_platforms=["codex"],
):
print(session.session_id, session.capture_sources)

Omit name to match a whole category: client, gateway, sdk or other. For example, CaptureSource(type="gateway") includes LiteLLM and Aperture. Selections use OR; other filters must also match. The filter covers each session's whole capture history, including duplicate deliveries and scrubbed sessions. It applies to the result count and every page. Omit the filter or pass None to select all; an empty list returns HTTP 422.

Session list, search and detail responses return capture_sources as a sorted, deduplicated list. A name can be absent when older data identifies only a category. Source metadata does not change conversation matching or permissions.

The separate sources: list[str] filter and response field describe retained messages, rather than every delivery. Duplicate messages keep the source first stored; removed messages no longer contribute. Scrubbed sessions return sources: [] and never match this legacy filter.

Retained-message keyMeaning
watcherMessages recorded by the Watcher client, or native transcripts stored with this source.
tailscale-apertureAperture imports and captures.
watcher-gatewayOther gateway captures.
watcher-sdkSDK captures.
watcher-hookOther hook captures.

New raw captures use explicit source metadata for these labels when supplied. Legacy hook messages use known transport markers or registered conversation-ID prefixes. Custom ingest source strings match exactly. The reserved watcher-hook source is accepted only by hook endpoints; ingest and ingest_append reject it. The sources filter also uses OR, rejects an empty list and can be omitted with None.

Inspect delivery paths​

Session list, search and detail responses include ingest_kinds, the sorted, distinct session capture paths. An empty list means no path was recorded; unknown values read as other. The list includes duplicate deliveries and survives content scrubbing.

get_session_tool_call_decision(session_id, tool_use_id) returns the current evaluation's ingest_kind, api_key_id and api_key_name. These fields also appear in trajectory decisions and standalone tool-call details. They require permission to read session findings. The API key name is a snapshot at the time of the current evaluation. Outcome-only updates preserve it. Tool-call search rows with available content also include ingest_kind and api_key_name. See tool-call decisions.

Search Tool Calls​

List individual tool calls rather than the sessions containing them:

from datetime import datetime, timedelta, timezone

end = datetime.now(timezone.utc)
start = end - timedelta(days=7)

with WatcherClient() as client:
offset = 0
while True:
response = client.search_tool_calls(
start_date=start,
end_date=end,
severities=["critical", "high"],
decision_categories=["human_denied", "auto_denied"],
limit=200,
offset=offset,
)
for call in response.tool_calls:
tool = call.tool_name if call.content_state == "available" else "(scrubbed)"
print(call.occurred_at, call.session_id, tool, call.decision_category, call.severity_score)
offset += len(response.tool_calls)
if not response.tool_calls or offset >= response.total:
break

Every filter is optional, and filters combine with AND. start_date and end_date must be timezone-aware. They match the time of the tool call's own message, not its session's span. session_id, user_id and tool_names narrow the list further. Without permission to read all session findings, user_id may name only yourself. limit is 1 to 1000 rows per page, with a default of 50. Page with offset until it reaches response.total. sort_order is desc (the default) or asc.

response.tool_calls holds the rows. Each row is one tool call Watcher recorded a permission decision about. Every row has tool_call_id, session_id, occurred_at, content_state, coverage_state, decision_category, severity_score, applied_decision, judged_decision, enforcement_mode, skip_reason (null unless a tool-call record explained the call), and the owner's user_id, name and email. Check content_state before reading tool details: available rows add tool_use_id, tool_name, tool_input_preview, decision_summary, ingest_kind and api_key_name. Argument previews require content access and are null without it. scrubbed rows retain only the content-free verdict, owner, and timestamp. Calls Watcher never saw are not listed.

With include_records=True, the list also holds metadata_only rows: calls for which Watcher holds only a tool-call record, not their content. They add tool_use_id and tool_name, and have no message to link to. A metadata_only row becomes available once the call's transcript arrives. The option is off by default, so existing integrations keep receiving only the rows they already handle.

coverage_state is decided when Watcher's pipeline ran, and otherwise names the gap: not_evaluated, grading_error or bypassed. With include_records=True it can also be skipped: the Watcher client observed the call but did not grade it, and skip_reason says why.

severities takes the severity bands critical, high, medium and low, plus not_scored for calls with no usable policy score. This can include ordinary unscored decisions and calls whose grading was bypassed, skipped, or failed. Read each row's coverage_state to distinguish those cases.

decision_categories takes the notable outcomes human_denied, auto_denied, human_allowed, would_denied and would_escalated, plus cleared. Routine calls match none of the notable outcomes. cleared selects the calls Watcher allowed without asking anyone: calls grading allowed, and calls your settings allow without grading, such as tools whose flag and deny thresholds are both disabled, and calls a command rule allows. In observe mode it also includes calls Watcher would have allowed, even if a person allowed or denied them in the coding agent's own permission prompt. Outside that observe-mode case, it excludes calls a person decided. It also excludes escalations, calls whose grading failed, calls Watcher did not evaluate, and calls a snooze or .watcher-ignore exempted.

sort_by takes severity, decision, occurred_at or tool_name. Sorting by decision orders by what Watcher did, with the calls it stopped first. Tool-name filters cannot match scrubbed rows because Watcher does not retain tool names after session content is removed.

Read scope follows the session endpoint requirements.

Get Session Detail​

Get a single session with its trajectories and grades:

with WatcherClient() as client:
session = client.get_session("your-session-uuid")
print(f"Session: {session.session_id}")
print(f"Time range: {session.started_at} — {session.ended_at}")
print(f"External IDs: {session.external_ids}")
for traj in session.trajectories:
print(f" Trajectory {traj.trajectory_id}: {traj.message_count} messages")

SessionSummary fields (in list/search responses):

  • session_id: UUID
  • trajectory_count, message_count: the session's trajectories and its longest trajectory's messages; for a conversation the Watcher client and a gateway both recorded, these count the reconciled transcripts, not the stored copies
  • started_at, ended_at: time range
  • external_ids: source-specific identifiers (e.g., {"anthropic-claude-code": "..."}), or None when none are stored (null in API JSON responses, including session detail)
  • metadata: values derived from the session's messages: hostname, entrypoint and agent_version, each present only when reported. See Session metadata for where each comes from and which value is kept
  • grades: most recent grade per grader (list of GradeResult)
  • max_score: highest score across all grades (or None)
  • ingest_kinds: sorted, distinct recorded delivery paths
  • capture_sources: recorded delivery categories and integration names, including duplicate uploads and scrubbed content
  • sources: sorted, distinct retained-message sources

Read Subagent Relationships​

client.get_session returns is_subagent and an authorized parent_session. parent_session is None when the parent is absent or inaccessible. Use client.list_session_subagents(session_id, limit=20, offset=0) for direct children, oldest first, with a maximum limit of 100. Results and the total exclude children the caller cannot read.

Titles require ownership or organization-wide content permission; review status and severity require findings access. Temporary approval allows opening the approved session but does not reveal navigation titles. See session content access for the access rules.

Request a Deep Review​

Deep Review runs on its own for recently active sessions with a gateway review score of 7 or higher, once they have been quiet for an hour. You can also request one for any session. The review covers the session's latest transcript and runs in the background, which can take several minutes.

import time

with WatcherClient() as client:
review = client.request_deep_review("your-session-uuid")
deadline = time.monotonic() + 3600
while review.status in ("queued", "running"):
if time.monotonic() > deadline:
raise SystemExit(f"Deep Review still {review.status} after an hour")
time.sleep(30)
review = client.get_deep_review_request("your-session-uuid", str(review.operation_id))
if review.status == "failed":
raise SystemExit(f"Deep Review failed; can_retry={review.can_retry}")

trajectory = client.get_trajectory("your-session-uuid", review.trajectory_hash)
report = [
g
for g in trajectory.grades
if g.trajectory_hash == review.trajectory_hash and g.grades.additional_properties.get("type") == "report"
]

Deep reviews, requested or scheduled, run on the operation worker, xylon-operation-worker, which requires PostgreSQL 17 or later. The Kubernetes Helm chart runs it by default, and Docker Compose always starts it. If the worker is not running, requested reviews stay queued, so the example stops polling after a deadline.

MethodHTTP request
request_deep_review(session_id)POST /v1/sessions/{session_id}/deep-review
get_deep_review_request(session_id, operation_id)GET /v1/sessions/{session_id}/deep-review/{operation_id}
get_current_deep_review(session_id)GET /v1/sessions/{session_id}/deep-review

Requests have no body. Requesting a review, polling it, and reading its report are separate access checks. See grading permissions and session read requirements for all three steps, including the example's get_trajectory call. A session you may not review or read returns 404 (WatcherNotFoundError), never 403. A scrubbed session also returns 404 to a request.

request_deep_review and get_deep_review_request return a DeepReviewRequestResponse:

  • operation_id: the ID to poll.
  • status: queued, running, succeeded or failed.
  • trajectory_hash: the transcript the review covers.
  • can_retry: always true. Requesting again after a failed review starts a new attempt, limited only by the organization's monthly grading cap; a queued, running or succeeded review is reused instead.

When the status is succeeded, the report is the session's Deep Review grade for that trajectory_hash. Read that trajectory even if the session has gained messages, and select a grade with type: report rather than a specific grader ID. This includes reports written by older releases. A newer review of the same session can replace it, leaving the report list empty. Unavailable session content returns 404. Asking again while the transcript is unchanged returns the same review. Asking after it failed starts a new attempt, with no limit per transcript beyond the organization's monthly grading limit. get_current_deep_review returns a wrapper whose current field holds a DeepReviewRequestResponse: any unfinished review of the session, whoever requested it, otherwise the latest review of its current transcript. current is None when no review was requested.

A request can fail with these WatcherAPIError status codes:

StatuscodeMeaning
402The organization is over its monthly grading limit.
409nothing_to_reviewThe session has no transcript yet. Try again after it records messages.
409review_unavailableThe session was scrubbed while the request was being handled.

A self-hosted deployment needs its background-job worker to run requested reviews; without it, they stay queued. See What drives the schedule when you self-host.

Trajectories​

List Trajectories in a Session​

with WatcherClient() as client:
response = client.list_trajectories("your-session-uuid")
for traj in response.trajectories:
print(f"{traj.trajectory_id}: {traj.message_count} messages, source={traj.source}")

Get Trajectory Detail​

Get the full conversation with messages and grades. The view parameter picks the payload shape and defaults to TrajectoryView.TRUNCATED, which elides heavy payloads behind marker fields; pass TrajectoryView.EXTENDED explicitly for storage-faithful message content:

from watcher_sdk import TrajectoryView

with WatcherClient() as client:
traj = client.get_trajectory("session-uuid", "trajectory-hash", view=TrajectoryView.EXTENDED)
print(f"Trajectory: {traj.trajectory_id}")
print(f"Source: {traj.source}")
print(f"Messages ({len(traj.messages)}):")
for msg in traj.messages:
print(f" [{msg.additional_properties.get('role', '?')}] {str(msg.to_dict())[:100]}...")
print(f"Grades: {len(traj.grades)}")

When the Watcher client and a gateway both recorded the conversation, the response is the reconciled transcript of both recordings: either recording's trajectory ID opens it, every tool call appears once, and each message's index is its position in that transcript.

TrajectoryDetailResponse fields:

  • trajectory_id: leaf hash identifier (for a reconciled transcript, the Watcher client recording's)
  • messages: list of message objects (role, content, metadata)
  • grades: list of GradeResult objects
  • member_trajectory_ids: the stored recordings this transcript covers, its own ID first
  • transcript_revision: for a reconciled transcript, the version a report's grades["input_revision"] must match to cite these messages; null otherwise
  • metadata: the stored metadata of the transcript's own last stored message only, not combined across messages like the session's metadata; its launch keys have other names (claude_code_entrypoint and claude_code_entry_version for Claude Code, originator and cli_version copied from the Codex session record, or cli_version copied from the Copilot session record). See Session metadata
  • session_id, source, created_at

Health and Version​

with WatcherClient() as client:
# GET /v1/health: "healthy", or "unhealthy" when the deployment's
# sign-in provider is unreachable (SSO mode)
health = client.health()
print(f"Status: {health.status} at {health.timestamp}")

# GET /v1/version: the build stamped into the connected deployment
build = client.version()
print(f"Server: {build.version} ({build.git_sha})")

Pagination​

Auto-paginate through all results with iterators:

with WatcherClient() as client:
# Iterate all sessions (fetches pages on demand)
for session in client.list_sessions_iter(page_size=50):
print(session.session_id)

# Iterate search results
for session in client.search_sessions_iter(
start_date=datetime(2026, 1, 1, tzinfo=timezone.utc),
end_date=datetime(2026, 2, 1, tzinfo=timezone.utc),
page_size=100,
):
print(session.session_id)
if some_condition:
break # stops pagination early

Async equivalents use async for:

async with AsyncWatcherClient() as client:
async for session in client.list_sessions_iter(page_size=50):
print(session.session_id)

Pages are fetched lazily: the iterator only makes HTTP requests as you consume items. Breaking out of the loop stops pagination immediately.

Error Handling​

All errors inherit from WatcherError:

WatcherError
├── WatcherConnectionError # Failed to connect to the API
├── WatcherTimeoutError # Request timed out
└── WatcherAPIError # API returned an error HTTP response
├── WatcherValidationError # 400, 422 — bad request
├── WatcherAuthenticationError # 401 — invalid/missing API key
├── WatcherNotFoundError # 404 — resource not found
├── WatcherRateLimitError # 429 — rate limited
└── WatcherRefreshTokenRotatedError # 503 from auth_refresh carrying a new refresh token

Status Code Mapping​

HTTP StatusException
400WatcherValidationError
401WatcherAuthenticationError
404WatcherNotFoundError
422WatcherValidationError
429WatcherRateLimitError
5xxWatcherAPIError
503 from auth_refresh(refresh_token=...) that sets a new refresh cookieWatcherRefreshTokenRotatedError

Error Attributes​

All WatcherAPIError subclasses have:

AttributeTypeDescription
status_codeintHTTP status code
errorstrError message from the API
messagestr | NoneHuman-readable message (for structured error bodies, the message; else the stringified detail)
codestr | NoneStable machine-readable error code from a structured body; branch on this
bodydict | NoneRaw parsed response body, lossless (None for SDK-synthesized errors)
request_idstr | NoneRequest ID for tracing

Structured error bodies ({"detail": {"code": ..., "message": ...}}) are flattened into code/message; the original shape stays available on body.

WatcherRateLimitError also has retry_after: float | None indicating how long to wait.

WatcherRefreshTokenRotatedError comes from auth_refresh(refresh_token=...) when the server answered 503 after it had already replaced the refresh token you sent. Store refresh_token: str from the error in place of the old token. The SDK does not retry this error. A call that relies on the client's cookie jar instead retries as usual, because the jar already holds the new token.

Examples​

from watcher_sdk import WatcherClient, WatcherNotFoundError, WatcherAPIError

with WatcherClient() as client:
try:
session = client.get_session("nonexistent-id")
except WatcherNotFoundError:
print("Session not found")
except WatcherAPIError as e:
print(f"API error {e.status_code}: {e.error}")
if e.request_id:
print(f"Request ID: {e.request_id}")

Catch specific errors for fine-grained handling:

from watcher_sdk import (
WatcherClient,
WatcherValidationError,
WatcherAuthenticationError,
WatcherRateLimitError,
)

with WatcherClient() as client:
try:
client.grade(session_id="bad-uuid", trajectory_ids=["traj-1"], grader_ids=["grader-uuid-1"])
except WatcherValidationError as e:
print(f"Invalid request: {e.message or e.error}")
except WatcherAuthenticationError:
print("Check your API key")
except WatcherRateLimitError as e:
if e.retry_after:
print(f"Rate limited. Retry after {e.retry_after}s")

Retry Behavior​

The SDK automatically retries transient failures with exponential backoff and jitter.

Retried automatically:

  • Connection errors (WatcherConnectionError)
  • Timeouts (WatcherTimeoutError)
  • Rate limits (WatcherRateLimitError / HTTP 429)
  • Bad gateway (502), service unavailable (503), gateway timeout (504)

Not retried (client errors):

  • Validation errors (400, 422)
  • Authentication errors (401)
  • Not found (404)
  • Other 4xx errors

Backoff strategy: Exponential with jitter, starting at 0.5s, max 10s between retries. Default 3 retries (configurable via max_retries).

To disable retry:

client = WatcherClient(max_retries=0) # no retries (1 attempt only)

Sync vs Async​

WatcherClient (sync)AsyncWatcherClient (async)
Use whenScripts, CLIs, Jupyter notebooksAsync applications, FastAPI routes
Context managerwith client:async with client:
Paginationfor item in client.list_sessions_iter():async for item in client.list_sessions_iter():
Event loopCreates its own (works in Jupyter)Uses the current event loop

The sync client works correctly in environments with existing event loops (e.g., Jupyter notebooks), unlike raw asyncio.run().

All methods have identical signatures and return types. The only difference is that async methods return coroutines (use await), and async iterators use async for.