Adding Scopes and Marks
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.
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
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.
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.
Python
Node.js
Rust
Save the following code as scopes_and_marks.py in your Quick Start project:
API references: nemo_relay.scope.scope and nemo_relay.scope.event.
Run it from the project directory:
Verify
Running the example prints:
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
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, andoutputpayloads. - 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.
- Add tool instrumentation with Instrument a Tool Call.
- Add model-provider instrumentation with Instrument an LLM Call.
- Export severity-tagged marks and metric measurements with OpenTelemetry.
- Use Code Examples for explicit scope-stack propagation helpers.