Code Examples
Use these examples when you need the direct runtime surfaces behind the application instrumentation guides.
Invocation API Selection
The following table shows which API to use based on your integration need:
Manual Tool Lifecycle
Use manual lifecycle calls only when the surrounding code owns the real tool invocation and only exposes reliable start and finish hooks.
If you are replaying events or bridging a framework clock, pass an explicit timestamp to the manual start, end, or mark helpers.
Python accepts timezone-aware datetime values, Node.js and WebAssembly accept Unix microseconds since epoch, Rust accepts DateTime<Utc>, and Go accepts time.Time.
Python
Node.js
Rust
Managed LLM Execution
Use managed execution when NeMo Relay should run the full middleware pipeline around the provider call.
Python
Node.js
Rust
Streaming LLM Execution
Use the streaming helper when subscribers need chunk collection plus one final response payload.
Python
Node.js
Rust
Partial Middleware Calls
These helpers are useful when framework code cannot use managed execution but still wants a request rewrite or block decision.
Python
Node.js
Rust
Scope and Context Helpers
Use normal scope helpers first. Reach for explicit stack helpers only when work crosses thread, task, worker, or request boundaries.
Python
Node.js
Rust
Middleware Registration Families
The runtime exposes the same registration families for tool and LLM calls:
- Sanitize-request guardrails change emitted start-event payloads only
- Sanitize-response guardrails change emitted end-event payloads only
- Conditional-execution guardrails return an allow-or-block decision
- Request intercepts change the real request before execution
- Execution intercepts wrap the callback and may post-process or short-circuit
- LLM stream execution intercepts wrap streaming provider callbacks
Every family also has a scope-local surface:
- Python:
nemo_relay.scope_local.register_* - Node.js:
scopeRegister* - Rust: middleware
scope_register_*functions undernemo_relay::api::registry; subscriber scope registration undernemo_relay::api::subscriber
Use Add Middleware for an end-to-end policy example and API Reference for symbol-level details.