Skip to navigation

Adding Scopes and Marks

View as MarkdownOpen in Claude

Use this guide to group related tool and LLM calls under one agent run, request, workflow, or operation, and to add optional checkpoint events.

The basic scope setup is included in Instrument a Tool Call and Instrument an LLM Call. Start with one of those Execute examples, then return here when you need more control over grouping or checkpoints.

What You Build

You will group one agent run in a scope, emit two checkpoint marks, and verify the events received by a subscriber.

A scope groups related work, such as an agent run, request, or workflow step. Tool calls, LLM calls, and marks attach to scopes so Relay can show which work belongs together.

A mark records a named checkpoint within that work, such as “plan created” or “retry scheduled.” It represents a single moment and can include data describing what happened.

1

Prepare

Complete one binding Quick Start guide first:

Choose the work you want to group. Common examples include:

  • One HTTP request
  • One agent run
  • One workflow step
  • One background job
  • One tenant-isolated experiment
2

Instrument

Register a temporary subscriber, then enter the scope before tool and LLM work begins. Emit marks for important checkpoints that do not wrap a callback, and close the scope when the agent run, request, or workflow ends. The highlighted lines below show the scope and mark calls in each binding.

3

Run

Run the complete example for your binding in the project from its Quick Start. Each example creates one agent-run scope, emits two marks, and prints the received events before removing the temporary subscriber.

Save the following code as scopes_and_marks.py in your Quick Start project:

scopes_and_marks.py
# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
import nemo_relay
def main() -> None:
"""Emit two checkpoint marks within an agent scope."""
def log_event(event: nemo_relay.Event) -> None:
print(f"{event.kind} {event.name}")
nemo_relay.subscribers.register("scope-check", log_event)
try:
with nemo_relay.scope.scope(
"agent-run",
nemo_relay.ScopeType.Agent,
input={"request_id": "req-123"},
) as handle:
nemo_relay.scope.event("planning-started", handle=handle, data={"step": 1})
nemo_relay.scope.event("planning-finished", handle=handle, data={"step": 2})
finally:
nemo_relay.subscribers.flush()
nemo_relay.subscribers.deregister("scope-check")
if __name__ == "__main__":
main()

API references: nemo_relay.scope.scope and nemo_relay.scope.event.

Run it from the project directory:

uv run python scopes_and_marks.py
4

Verify

Running the example prints:

Expected output
scope agent-run
mark planning-started
mark planning-finished
scope agent-run

The two scope agent-run lines are the start and end of the same scope. The named marks are checkpoints within it.

Check that the subscriber prints:

  • One scope start event for agent-run
  • One mark event for planning-started
  • One mark event for planning-finished
  • One scope end event for agent-run
Flush Subscriber Delivery

The examples flush subscriber delivery before removing the temporary subscriber. Keep this cleanup so queued events are delivered before the program exits. If a subscriber callback emits another event, flush again before checking the output.

If marks appear outside the intended trace, pass the active scope handle explicitly or make sure the mark is emitted while the scope is active.

When to Add Marks

Use marks for checkpoints that should appear in the event stream but do not wrap a callback:

  • Request accepted
  • Plan created
  • Memory loaded
  • Routing decision made
  • Final answer assembled
  • Retry scheduled

Use a nested scope instead of a mark when the work has a meaningful start and end boundary, child work, or a duration that matters.

Emit Typed Marks and Metrics

The generic mark API can attach a data_schema describing the opaque data payload and a typed log severity. In Python, pass data_schema= and severity= to scope.event; in Node.js, pass dataSchema and severity as the final event arguments; and in Rust, set data_schema and severity on EmitMarkEventParams. These options let OTLP log export preserve the schema and severity without requiring application-defined metadata keys.

Use the binding’s metric helper for a validated metric group: scope::metric() in Rust, scope.metric() in Python, or metric() in Node.js. See Emit Log and Metric Marks for the metric measurement shape and routing rules.

Production Checklist

Use this checklist before running the pattern in production traffic.

  • Keep scope names stable enough for filtering and dashboards.
  • Use marks for business checkpoints, not verbose debug logging.
  • Include only JSON-compatible data, metadata, input, and output payloads.
  • Do not attach sensitive payloads unless a mark or scope event sanitizer removes them before subscriber and exporter delivery.
  • Propagate the active scope stack when work crosses thread or worker boundaries.

Next Steps

Use these links to continue from this workflow into the next related task.