Files
hermes-agent/website/docs/developer-guide/observer-hooks.md
unsupportedpastels 06296e269e feat(plugins): tell plugins which prompt a gateway session is waiting on
Add two observer hooks, on_session_prompt and on_session_prompt_settled,
fired by the gateway session owner for each prompt a person answers for a
session's viewers, with the id that answers it:

- approval / clarify: when SessionAuthority.register_approval /
  register_clarify has published the approval.request / clarify.request
  event (prompt_id = the event's prompt_id); settled whenever the prompt
  leaves PendingControls.pending (snapshot, a remote_responders answer, a
  managed worker's prompt_settled frame), reason "settled".
- request frames: when session_server_requests._ask has opened and written a
  sudo, secret, clarify (batch), vault.unlock_prompt, vault.code or
  vault.save_login frame (prompt_id = the srq- id); settled after its
  request.cancel with that event's reason. Window requests, replay=False
  requests and an _ask that returns early never fire.

params is a deep copy of what the viewers received with every string
force-redacted through redact_approval_display(force=True); it never holds
an answer. Calls are queued to one FIFO worker thread, so plugin code never
runs under event_stream.lock or the request registry lock, on the agent
thread or on the event loop, and each runs in the owning profile's
owner_scope (for a request frame: the served authority whose live session
owns the stream, else the asker's context). Plugin exceptions are logged
at debug and swallowed. Event payloads, publish order, open_requests
replay, return values and on_human_input_* are unchanged.

Both names join VALID_HOOKS and are documented in the observer-hooks,
hooks, plugins and plugin developer guide pages.
2026-11-23 23:16:56 -05:00

16 KiB

title, description
title description
Observer Hooks Read-only telemetry contract for plugins: event families, correlation IDs, payload safety

Hermes Observer Hooks

Hermes observer hooks are the read-only telemetry contract for plugins that need to reconstruct agent execution without changing runtime behavior. This contract supports trace, metrics, audit, replay, and export integrations such as Langfuse, OpenTelemetry-style collectors, and NeMo Relay.

Observer hooks are intentionally backend-neutral. They expose stable lifecycle events, correlation IDs, sanitized payloads, timing, status, and error fields. They do not replace Hermes' planner, model providers, memory, tool registry, approval UX, CLI, gateway behavior, or execution semantics.

Behavior-changing request or execution wrappers are outside this observer contract. Observer hooks should report what happened; they should not replace provider requests, tool arguments, or execution callbacks.

Hermes also has a first-party NeMo Relay shared-metrics path. It uses these lifecycle boundaries directly and does not require enabling an observability plugin. See Relay shared metrics.

Contract

Plugins register observer callbacks from register(ctx):

def register(ctx):
    ctx.register_hook("pre_api_request", on_pre_api_request)
    ctx.register_hook("post_api_request", on_post_api_request)
    ctx.register_hook("pre_tool_call", on_pre_tool_call)
    ctx.register_hook("post_tool_call", on_post_tool_call)

Every hook callback receives keyword arguments. Plugins should accept **kwargs so additive fields remain backward-compatible:

def on_post_tool_call(**kwargs):
    tool_name = kwargs.get("tool_name")
    status = kwargs.get("status")
    result = kwargs.get("result")

The plugin manager injects this field into every hook payload:

telemetry_schema_version = "hermes.observer.v1"

Hook callbacks are fail-open. Hermes catches callback exceptions, logs a warning, and keeps the agent loop running.

Most observer hook return values are ignored. The exceptions are older behavior-affecting hooks:

Hook Return behavior
pre_llm_call May return a string or {"context": "..."} to inject ephemeral context into the current user message, or {"action": "block", "message": "..."} to end the turn before any inference request (the message becomes the reply and the prompt is stored as a withheld placeholder; see Event Hooks). A callback that raises or times out is skipped, so it never blocks.
pre_tool_call May return {"action": "block", "message": "..."} to block a tool before execution, or {"action": "modify", "args": {...}} to transform the tool's input arguments.
transform_tool_result May return a replacement tool result string after post_tool_call.
transform_llm_output May return a replacement final assistant text string.

Telemetry plugins should treat these behavior-affecting returns as optional compatibility features, not as observability requirements.

Correlation IDs

Observer payloads use stable IDs so plugins can join events without relying on callback order alone.

Field Meaning
session_id Conversation/session identity.
task_id Task identity, especially useful for subagents and isolated execution.
turn_id User-turn identity shared by API attempts and tool calls in a turn.
api_request_id Opaque provider-attempt identity. Do not parse its string format.
api_call_count Numeric API attempt count within the agent loop.
tool_call_id Provider-supplied tool call ID when available.
parent_session_id / child_session_id Session link for delegated subagents.
parent_subagent_id / child_subagent_id Subagent link when available.
parent_turn_id Parent turn that spawned delegated work.

Consumers should prefer explicit fields over parsing compound IDs. In particular, api_request_id is an opaque correlation value.

Event Families

Session Lifecycle

Session hooks describe conversation boundaries and resets:

Hook When it fires
on_session_start A brand-new session starts after the system prompt is built.
on_session_end A run_conversation call ends, including interrupted or incomplete turns.
on_session_finalize CLI or gateway tears down an active session identity.
on_session_reset CLI or gateway moves from an old session identity to a new one.
on_session_delete A session delete or deliberate archive committed (reason says which).

Common fields include session_id, completed, interrupted, reason, old_session_id, and new_session_id where available.

on_session_end is turn/run scoped. It is not necessarily the final lifetime boundary for a chat identity. Use on_session_finalize and on_session_reset for lifecycle cleanup that must happen once per session identity.

Turn-Scoped LLM Hooks

These hooks frame the user turn, not individual provider API attempts:

Hook When it fires
pre_llm_call Before the tool loop begins for a user turn.
post_llm_call After the turn completes with final assistant output.

Common pre_llm_call fields include session_id, turn_id, user_message, conversation_history, is_first_turn, model, platform, and sender_id.

Common post_llm_call fields include session_id, turn_id, user_message, assistant_response, conversation_history, model, and platform.

Use request-scoped API hooks for LLM span telemetry. Use pre_llm_call and post_llm_call for turn-level context, compatibility, and final turn summary.

Request-Scoped API Hooks

API hooks describe provider attempts inside the agent loop:

Hook When it fires
pre_api_request Immediately before a provider API request.
post_api_request After a successful provider response.
api_request_error After a failed provider request or retryable error path.
pre_auxiliary_call Before each provider attempt of an auxiliary LLM call (titling, compression, MoA, vision, ...). Carries aux_task; the *_api_request hooks stay main-loop only.
post_auxiliary_call After that attempt returns or raises (error set on failure).

pre_api_request includes:

  • identity: session_id, task_id, turn_id, api_request_id
  • runtime: platform, model, provider, base_url, api_mode
  • attempt metadata: api_call_count, message_count, tool_count, approx_input_tokens, request_char_count, max_tokens
  • timing: started_at
  • sanitized request payload: request

post_api_request includes the same identity/runtime fields plus:

  • api_duration, started_at, ended_at
  • finish_reason, message_count, response_model
  • usage
  • assistant_content_chars, assistant_tool_call_count
  • sanitized response payload: response
  • compatibility object: assistant_message

api_request_error includes the same identity/runtime fields plus:

  • api_duration, started_at, ended_at
  • status_code, retry_count, max_retries, retryable, reason
  • structured error = {"type": ..., "message": ...}
  • sanitized failed request payload: request

The sanitized request, response, and error fields are the canonical observer inputs for new consumers.

Tool Lifecycle

Tool hooks describe individual tool calls:

Hook When it fires
pre_tool_call Before guardrail-approved tool dispatch.
post_tool_call After tool dispatch, cancellation, block, or error completion.
transform_tool_result After post_tool_call, before the result is appended to model context.

pre_tool_call includes tool_name, args, task_id, session_id, tool_call_id, turn_id, and api_request_id.

post_tool_call includes the same identity fields plus result, duration_ms, status, error_type, and error_message.

status is the observer-grade lifecycle outcome. Common values include:

Status Meaning
ok Tool completed normally.
error Tool ran and returned or raised an error outcome.
blocked A pre_tool_call hook blocked execution.
cancelled Execution was cancelled before normal completion.

post_tool_call is emitted for blocked and cancelled paths so telemetry plugins can close spans cleanly.

Human Input Lifecycle

on_human_input_request / on_human_input_resolved fire around every point where the agent blocks for a person: sudo password prompts (kind="sudo"), clarify questions (kind="clarify") and approval prompts (kind="approval") on CLI, TUI/Desktop, ACP and gateway platforms. Fields: kind, request_id (shared by the pair), session_id, session_key, platform, and a force-redacted prompt; the resolved hook adds outcome. The typed password or answer is never included. Smart (aux-LLM) approvals do not fire it. Source: tools/human_input_hooks.py.

Session Prompt Lifecycle

on_session_prompt / on_session_prompt_settled fire in the gateway when the session owner opens a prompt that a person answers for a session's viewers (CLI, Ink TUI, Desktop or a phone app attached to that session), and again when it stops being open. Unlike on_human_input_request, each call carries the id that answers the prompt:

kind Opened when the owner prompt_id answers it through
approval publishes an approval.request event approval.respond
clarify publishes a clarify.request event clarify.respond
sudo, secret, clarify, vault.unlock_prompt, vault.code, vault.save_login writes that server-to-client request frame (clarify here is the batch panel) a response frame with that srq- id, or clarify.lock

Window requests a client app answers on its own (theme.*, tour, preview.*, window.read, terminal.read, setup_beat) and requests that are never replayed to a reconnecting viewer do not fire, and neither does a prompt nobody could be asked (no viewer attached).

Fields: session_id, kind, prompt_id, execution_generation (the turn's generation for approval / clarify, None for request frames), and on the opened hook params: a deep copy of what the viewers received, with every string force-redacted the way on_human_input_request redacts prompt (even with security.redact_secrets: false). The settled hook adds reason: the request.cancel reason for a frame (resolved, timeout, interrupted) or settled for an approval.settled / clarify.settled event. What the person typed is never included. Each prompt id gets exactly one opened and one settled call, in that order.

Calls are queued and run in order on one worker thread, never under the session's locks, on the agent thread or on the event loop, and always in the scope of the profile that owns the session, so on a multi-profile gateway only that profile's plugins see them. A slow callback delays later calls of these two hooks, nothing else.

Use these hooks when you need to answer or track a specific prompt of a gateway session (a notification someone answers in place); use on_human_input_request for a surface-neutral "Hermes is waiting on you" signal, which also covers messaging platforms and the CLI's in-process prompts but has no answerable id and fires once per clarify call. Source: gateway/session_prompt_hooks.py.

Approval Lifecycle

Approval hooks describe dangerous-command approval prompts:

Hook When it fires
pre_approval_request Before the approval request is shown or sent.
post_approval_response After the user responds or the request times out.

Common fields include command, description, pattern_key, pattern_keys, session_key, and surface.

post_approval_response also includes choice, with values such as once, session, always, deny, timeout, and cancelled (nobody answered: the prompt was withdrawn — turn interrupted or ended — or never reached the user on the CLI).

Approval hooks are observer-only. Plugins cannot pre-answer or veto approvals from these hooks. To prevent a tool from reaching approval, use pre_tool_call blocking.

Subagent Lifecycle

Subagent hooks describe delegated child-agent work:

Hook When it fires
subagent_start A delegated child agent is created.
subagent_stop A delegated child agent returns or fails.

subagent_start fields include parent_session_id, parent_turn_id, parent_subagent_id, child_session_id, child_subagent_id, child_role, and child_goal.

subagent_stop fields include parent/child session IDs, role/status fields, child_summary, duration_ms, and a metadata-only tool_call_history. Each history entry contains the tool name, argument names, bounded side-effect targets, input/output byte counts, and outcome. URL query strings and fragments are removed; raw arguments, prompts, commands, contents, headers, and results are intentionally excluded.

Observers can use these hooks to model nested trajectories while keeping child agent execution linked to the parent turn that spawned it.

Payload Safety

Observer payloads are designed for telemetry consumers, not raw object access. New consumers should use the sanitized API payloads:

  • pre_api_request.request
  • post_api_request.response
  • api_request_error.request
  • api_request_error.error

Sanitization converts provider objects to JSON-compatible structures, bounds large payloads, redacts sensitive keys, and avoids exposing raw response objects in sanitized fields.

Legacy compatibility fields such as request_messages, conversation_history, and assistant_message may still be present for existing plugins. New observability consumers should prefer the sanitized payloads.

Performance

The default uninstrumented path should stay cheap. Expensive request/response payload construction is gated behind has_hook(...), so Hermes only builds sanitized API telemetry payloads when at least one plugin registered the relevant hook.

Plugin authors should preserve this property:

  • Register only hooks the plugin actually consumes.
  • Avoid deep-copying or re-sanitizing already sanitized payloads.
  • Keep hook callbacks fast and fail-open.
  • Offload network export or batch writes when practical.

Writing An Observer Plugin

Minimal observer plugin:

def register(ctx):
    ctx.register_hook("pre_api_request", on_pre_api_request)
    ctx.register_hook("post_api_request", on_post_api_request)
    ctx.register_hook("pre_tool_call", on_pre_tool_call)
    ctx.register_hook("post_tool_call", on_post_tool_call)


def on_pre_api_request(**kwargs):
    start_llm_span(
        request_id=kwargs.get("api_request_id"),
        turn_id=kwargs.get("turn_id"),
        request=kwargs.get("request"),
        model=kwargs.get("model"),
    )


def on_post_api_request(**kwargs):
    finish_llm_span(
        request_id=kwargs.get("api_request_id"),
        response=kwargs.get("response"),
        usage=kwargs.get("usage"),
        duration=kwargs.get("api_duration"),
    )


def on_pre_tool_call(**kwargs):
    start_tool_span(
        call_id=kwargs.get("tool_call_id"),
        name=kwargs.get("tool_name"),
        args=kwargs.get("args"),
    )


def on_post_tool_call(**kwargs):
    finish_tool_span(
        call_id=kwargs.get("tool_call_id"),
        result=kwargs.get("result"),
        status=kwargs.get("status"),
        duration_ms=kwargs.get("duration_ms"),
    )

Use session_id, turn_id, api_request_id, and tool_call_id for span correlation. Use subagent and approval hooks when the export format supports nested agent work or security lifecycle events.

Existing Consumers

The official Langfuse catalog plugin demonstrates direct hook-based observability for turns, provider requests, and tool calls.

The native NeMo Relay SDK integration maps Hermes session, turn, LLM, and tool lifecycles to Relay. Relay's discovered user and system configuration, or an explicit file selected with HERMES_NEMO_RELAY_PLUGINS_TOML, can add ATOF, ATIF, or OTEL exporters and execution middleware; see Relay shared metrics.