agent() automatically captures ai.agent.input, ai.agent.output, and timing. To filter runs by user, session, or environment in the Lemma dashboard, attach custom attributes on the run span inside the wrapped function.
Setting attributes on the run span
Usectx.span.setAttribute() / ctx.span.set_attribute() to set attributes at any point inside the wrapped function:
- TypeScript
- Python
Recommended attribute keys
| Key | Description |
|---|---|
lemma.user_id | Unique identifier for the end user |
lemma.session_id | Session or conversation identifier |
lemma.environment | Deployment environment, e.g. "production", "staging" |
lemma.feature | Feature area, e.g. "support_chat", "onboarding" |
lemma.* prefix is just a convention for attributes you intend to filter on.
Python context manager
In context manager mode, access the span through the yielded run object:Attributes on child spans
Child spans (tool calls, LLM steps) can also carry custom attributes. Set them via the OTel span API directly inside the helper function:Next Steps
- Overview — the
agent()root span and what it captures automatically - Multi-turn threads — link related runs into a conversation thread
- Manual instrumentation — typed span helpers for tool, LLM, and retrieval child spans

