Learn how sessions, turns, and steps work
Lens
Debug AI agent answers by tracing the context behind each turn, from retrieval to prompt.
What is Lens?
Lens is a Redis Iris observability tool that traces your AI agent's turns. A turn is one agent exchange, such as answering a single message. For each turn, Lens records what each source returned, such as a memory recall or a retrieval, and what entered the prompt. It also records which model and tool calls ran, how long each step took, and how the turn ended.
When your agent gives a wrong answer, Lens shows where it went wrong. For example, you might see that the right memory was recalled but dropped from the prompt, or that a retrieval came back empty.
Trace your first turn and read it back
Trace agents built with LangGraph, the OpenAI Agents SDK, and more
Why use Lens?
- See why an agent answered the way it did: Compare what each source returned with what entered the prompt, turn by turn.
- Iris services trace themselves: Agent Memory, Agent Playbooks, and Context Gateway calls appear in the turn with no extra code.
- Model calls traced automatically: Calls through OpenAI, Anthropic, Amazon Bedrock, and LangChain clients appear with their prompts, completions, and token counts.
- Built on OpenTelemetry: Lens joins the tracer your application already uses, and Iris service steps reach your existing observability tools as well.
- You control content capture: Capture prompts and retrieved text for debugging, or keep only IDs, hashes, and counts.
- Background export: Lens exports in the background, so your agent never waits for it. If an export fails, the error stays out of your agent's code.
Concepts
Lens groups turns into sessions by agent, and records each turn's steps. See Lens concepts.
- Your agent opens a turn for each message with the Redis Iris software development kit (SDK).
- The SDK records each step in the turn: retrievals, memory recalls, prompt assembly, model calls, tool calls, and Iris service calls.
- Lens assembles the turn and groups it into sessions by agent.
- You inspect the turn in the Lens user interface (UI) or query it with the read application programming interface (API).
Quick example
-
Set up a Lens instance and get its endpoints and keys:
- Redis Cloud: Create Lens.
- Self-managed: Deploy Lens on Kubernetes.
-
Install the Redis Iris Python package:
pip install 'redis-iris[llm-tracing]>=1.0.0' -
With
IRIS_LENS_ENDPOINT,IRIS_LENS_API_KEY, andIRIS_LENS_AGENT_IDin your environment, trace a turn. The example calls OpenAI; use any supported client.
import uuid
from openai import OpenAI
from redis_iris import lens
lens.init()
client = OpenAI()
session_id = f"quickstart-{uuid.uuid4().hex[:8]}"
with lens.turn(session_id, user_id="user-1042") as turn:
response = client.responses.create(
model="gpt-4.1-mini",
input="Where can I see my refund status?",
)
print(response.output_text)
turn.set_outcome("completed")
turn_id = turn.trace_id
print(f"Session: {session_id}")
print(f"Turn: {turn_id}")
import uuid
from openai import OpenAI
from redis_iris import lens
lens.init()
client = OpenAI()
session_id = f"quickstart-{uuid.uuid4().hex[:8]}"
with lens.turn(session_id, user_id="user-1042") as turn:
response = client.responses.create(
model="gpt-4.1-mini",
input="Where can I see my refund status?",
)
print(response.output_text)
turn.set_outcome("completed")
turn_id = turn.trace_id
print(f"Session: {session_id}")
print(f"Turn: {turn_id}")
import os
import time
from redis_agent_lens import Lens, errors
with Lens(
server_url=os.environ["LENS_URL"],
instance_id=os.environ["LENS_INSTANCE_ID"],
) as reader:
for _ in range(30):
try:
detail = reader.get_turn(turn_id=turn_id)
break
except errors.NotFoundErrorResponseContent:
time.sleep(2)
else:
raise RuntimeError("The turn isn't readable. Check the iris.lens warnings for an export error.")
print(f"Outcome: {detail.outcome.value}")
print(f"Tokens: {detail.tokens.total}")
Follow the quickstart to read the turn back.
Use Lens
- Instrument an agent: record turns, retrievals, prompt assembly, and custom steps.
- Integrate with an agent framework: trace LangGraph, OpenAI Agents SDK, Google Agent Development Kit (ADK), and your own loop.
- Inspect sessions: find a session and read its turns in the Lens UI.
- Configure Lens: settings, keys, content capture, and OpenTelemetry.
- Query sessions and turns: read turns from code.
- Limits and errors: request limits, export behavior, and troubleshooting.
- API reference: the read API operations and data models.