Context Managers
Fine-grained tracing control with context managers.
Context managers provide fine-grained control over tracing scope and attributes.
JavaScript SDK?
Session Context
Group related traces into a user session:
from risicare import session_context
async def handle_request(user_id: str, request_id: str):
with session_context(
session_id=request_id,
user_id=user_id,
metadata={"source": "api"},
parent_session_id=None,
turn_number=1,
):
# All traces here belong to this session
await process_request()Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
session_id | str | required | Unique session identifier |
user_id | str | None | None | Optional user identifier |
metadata | dict | None | None | Custom session metadata |
parent_session_id | str | None | None | Link to previous turn in multi-turn conversations |
turn_number | int | 1 | Turn number in multi-turn conversation |
Async Variant
from risicare import async_session_context
async def handle_request(user_id: str, request_id: str):
async with async_session_context(
session_id=request_id,
user_id=user_id,
):
await process_request()Session outside, run inside
Open the session first. Then open the run (@agent, @trace or with risicare.trace(...)) inside it:
import risicare
from risicare import async_session_context
async def handle_message(session_id: str, user_id: str, message: str):
async with async_session_context(session_id=session_id, user_id=user_id): # 1. the session: outside
with risicare.trace(name="support-turn"): # 2. the run: inside
return await answer(message)The SDK writes the session on each span that starts inside the session. A run span that starts before the session does not get it. For JavaScript, see Session outside, run inside.
The session.end marker. Each time a session_context() block, or a @session call that has a session ID, ends, the SDK sends one session.end marker span: one marker for each call. With the session outside the run, no span is open at that moment, so the marker is a one-span trace of its own. It is never a failed trace (since Python 0.16.1): its status is ok, not error, and the outcome is in risicare.lifecycle.status (ok, error or cancelled). Through Python 0.16.0, a failed turn made the marker an error span too, so with the session outside the run one failed request read as two failed traces. See Sessions.
Agent Context
Identify agent boundaries within a trace:
from risicare import agent_context
def run_planner():
with agent_context(
"planner-001", # agent_id (required, positional)
agent_name="planner",
agent_role="orchestrator",
agent_type="custom",
version=1,
metadata={"model": "gpt-4o"},
):
# All spans here are attributed to this agent
plan = create_plan()
return planParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
agent_id | str | required | Unique agent instance ID |
agent_name | str | None | None (falls back to agent_id) | Human-readable agent name |
agent_role | str | None | None | Agent role (orchestrator, worker, specialist) |
agent_type | str | None | None | Framework type (langgraph, crewai, autogen, custom) |
version | int | None | None | Agent version number |
metadata | dict | None | None | Custom agent metadata |
Automatic Parent Tracking
When agent contexts are nested, the inner agent automatically records the outer agent as its parent_agent_id. You do not need to set this manually.
Async Variant
from risicare import async_agent_context
async def run_worker():
async with async_agent_context(
"worker-001",
agent_name="worker",
agent_role="specialist",
):
await do_work()Phase Context
Track decision phases using the SemanticPhase enum:
from risicare import phase_context
from risicare import SemanticPhase
def process_task(task: str):
# Thinking phase
with phase_context(SemanticPhase.THINK):
analysis = analyze_task(task)
# Decision phase
with phase_context(SemanticPhase.DECIDE):
action = select_action(analysis)
# Action phase
with phase_context(SemanticPhase.ACT):
result = execute_action(action)
return resultSemanticPhase Values
| Phase | Value | Description |
|---|---|---|
SemanticPhase.THINK | "think" | Reasoning, planning, analysis |
SemanticPhase.DECIDE | "decide" | Decision making, action selection |
SemanticPhase.ACT | "act" | Action execution, tool calls |
SemanticPhase.REFLECT | "reflect" | Self-evaluation |
SemanticPhase.OBSERVE | "observe" | Environment observation, state reading |
SemanticPhase.COMMUNICATE | "communicate" | Inter-agent communication |
SemanticPhase.COORDINATE | "coordinate" | Multi-agent coordination |
Enum Required
phase_context accepts a SemanticPhase enum value, not a plain string. Use SemanticPhase.THINK instead of "think".
Span Context
Create custom spans for any operation. tracer.start_span() returns a ManagedSpan that supports both context manager and standalone usage.
Context manager (recommended):
from risicare import get_tracer
def complex_operation():
tracer = get_tracer()
with tracer.start_span(
"data_processing",
attributes={"batch_size": 100},
) as span:
result = process_data()
span.set_attribute("records_processed", len(result))
return resultStandalone (manual lifecycle):
tracer = get_tracer()
span = tracer.start_span("my-operation")
span.set_attribute("model", "gpt-4o")
result = llm.invoke(prompt)
span.end()Context manager vs standalone
The context manager form automatically manages the span lifecycle and context propagation. The standalone form requires you to call span.end() manually and does not set the span as the current context.
Parameters
| Parameter | Type | Description |
|---|---|---|
name | str | Span name |
kind | SpanKind | Span kind (e.g. SpanKind.INTERNAL, SpanKind.LLM_CALL) |
attributes | dict | None | Custom span attributes |
Nesting Contexts
Contexts can be nested to create rich hierarchies:
from risicare import session_context, agent_context, phase_context
from risicare import SemanticPhase
async def handle_user_request(user_id: str, request: str):
with session_context(session_id=f"sess-{user_id}"):
with agent_context("orchestrator-001", agent_name="orchestrator", agent_role="orchestrator"):
with phase_context(SemanticPhase.THINK):
intent = await classify_intent(request)
with phase_context(SemanticPhase.DECIDE):
specialist = select_specialist(intent)
with agent_context("specialist-001", agent_name=specialist, agent_role="specialist"):
with phase_context(SemanticPhase.ACT):
result = await execute_task(request)
return resultAsync Support
session_context and agent_context have async variants (async_session_context, async_agent_context). The other context managers have no async variant: use them with a plain with, which also works inside async def.
from risicare import async_agent_context, async_session_context
async def async_agent():
async with async_session_context("sess-123", user_id="u-456"):
async with async_agent_context("worker-001", agent_name="async-worker"):
result = await async_operation()
return resultThread Safety
Context managers use contextvars for thread-safe propagation. When auto_patch=True (the default), risicare.init() patches ThreadPoolExecutor, ProcessPoolExecutor, and asyncio.create_task so context propagates automatically:
import concurrent.futures
from risicare import agent_context, get_current_agent
def worker(task_id: int):
# Context is automatically propagated from the submitting thread
agent = get_current_agent()
return process_task(task_id)
with agent_context("pool-coordinator", agent_role="orchestrator"):
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(worker, i) for i in range(10)]
results = [f.result() for f in futures]One run, or one run for each item. With auto_patch (the default), a task that you submit to a ThreadPoolExecutor or a ProcessPoolExecutor continues the trace that is open when you submit it: each task is a child of that trace. So a with risicare.trace(...) around the whole batch makes the batch ONE run, and one failed task makes that run read failed. For one run for each item, submit with no trace open, and open the trace inside the task (with risicare.trace(name=...), or @risicare.trace on the task function):
import concurrent.futures
import risicare
@risicare.trace(name="process-item") # one run for each item
def process_item(item):
return handle(item)
with concurrent.futures.ThreadPoolExecutor() as executor: # no trace is open here
results = list(executor.map(process_item, items))A multiprocessing.Pool is not patched: its tasks never continue the trace of the parent.
Process Pool Propagation
A multiprocessing.Pool task, or a task that you submit with auto_patch=False, does not get the context. Use get_trace_context() and restore_trace_context() to send it across the process boundary yourself. In a pool worker, call risicare.flush() at the end of each task (see Production & Failure Modes):
from risicare import get_trace_context, restore_trace_context
ctx = get_trace_context()
def worker(trace_ctx):
with restore_trace_context(trace_ctx):
do_work()
executor.submit(worker, ctx)Cross-Service Context Propagation
When your application spans multiple services (e.g., Service A calls Service B via HTTP), trace context must be propagated via HTTP headers so spans from both services appear in the same trace.
Risicare uses the W3C Trace Context standard (traceparent and tracestate headers).
Inject (caller side)
inject_trace_context() reads the current span, session, and agent from ContextVars. It adds a traceparent header when a span is current, and a tracestate header when a session or an agent is current. Outside a span it adds nothing:
import httpx
from risicare import inject_trace_context, trace
@trace("call-service-b")
def call_service_b(payload: dict):
headers = {"Content-Type": "application/json"}
inject_trace_context(headers)
# headers now contains:
# traceparent: 00-{trace_id}-{span_id}-01
# tracestate: risicare=session_id={...};agent_id={...} (only when a session or an agent is current)
return httpx.post("https://service-b/api/process", headers=headers, json=payload)Extract (receiver side)
extract_trace_context() parses the W3C headers and returns a dict with version, trace_id, parent_span_id and flags, plus session_id and agent_id when the caller sent them. It looks up the header names in lower case:
restore_trace_context() expects a TraceContext object, so build one from the extracted dict first (note span_id comes from the dict's parent_span_id key):
from risicare import TraceContext, extract_trace_context, restore_trace_context
# In your HTTP handler (FastAPI, Flask, etc.)
def handle_request(request):
raw = extract_trace_context({k.lower(): v for k, v in request.headers.items()})
# raw = {"trace_id": "abc...", "parent_span_id": "def...", "session_id": "..."}
# Require both ids; a malformed header falls through to a fresh trace
if raw.get("trace_id") and raw.get("parent_span_id"):
ctx = TraceContext(
trace_id=raw["trace_id"],
span_id=raw["parent_span_id"],
session_id=raw.get("session_id"),
agent_id=raw.get("agent_id"),
)
with restore_trace_context(ctx):
# All spans created here belong to the same trace as the caller
return process(request)
return process(request)tracestate carries Risicare metadata
The tracestate header carries Risicare-specific state (session ID, agent ID) using ; as the internal delimiter to avoid collision with the W3C vendor delimiter ,. This is parsed automatically by extract_trace_context().