what is best way to send the chat history for each...
# ask-ai
b
what is best way to send the chat history for each turn in some tracing or service where i cna observer the chat and each turn response
t
Thanks for reaching out, Nitish. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you send chat history for each turn to a tracing or observation service! LiveKit offers some handy built-in telemetry utilities that make it easy to convert your chat context into OpenTelemetry-compatible events. From there, you can send these events to external tracing platforms like LangFuse, Honeycomb, or any OpenTelemetry backend. Here’s a simple overview of how you can do this: • You can access the chat history through
session.chat_ctx
, which holds all messages, function calls, and outputs. • Then, use the
_chat_ctx_to_otel_events
utility to convert the chat context into OpenTelemetry events. • Attach these events to spans by using
trace.get_current_span()
and
span.add_event()
. • Make sure to enable tracing with a provider using
set_tracer_provider()
, and you can also add helpful metadata like session or user IDs. • For real-time updates, the
conversation_item_added
event is great for capturing incremental changes. • If you’re working with models like OpenAI Realtime, just enable the
tracing
parameter in the model options for smooth automatic integration. We also have a complete example integration with LangFuse available here. It walks you through setting up a tracer provider, sending chat messages as events, and capturing session-level spans. If you’d like, I’m here to assist you with setting up a specific tracing backend or integrating with any particular LLM provider. Just let me know how I can help! Sources: Workflows | LiveKit Docs | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | Logs, metrics, and telemetry | LiveKit Docs
b
is there nay exmaple that i can see ?
t
Absolutely! We have a complete example ready for you that shows how to integrate with LangFuse. It walks you through setting up a tracer provider, sending chat messages as events, and capturing session-level spans. You can check out the example here. If you have any questions while going through it or if you'd like examples for other tracing backends, please don’t hesitate to reach out. I’m here to help!
b
the exmaple is see does nto log the chat, and it does not also log the out put been genertaed
t
Thank you for sharing the example! It’s great that the tracing infrastructure is set up. To capture the chat history and generated outputs as well, you can enhance the example by logging the full chat context and outputs as OpenTelemetry events. Here’s a friendly step-by-step on how to do this: 1. First, access the chat history with
session.chat_ctx
. This holds all your messages, function calls, and outputs. 2. Next, use the
_chat_ctx_to_otel_events
utility to transform that chat context into OpenTelemetry events. 3. Then, attach these events to the current span by calling
span.add_event()
. For example:
Copy code
from opentelemetry import trace

span = trace.get_current_span()
chat_events = _chat_ctx_to_otel_events(session.chat_ctx)
for name, attrs in chat_events:
    span.add_event(name, attrs)
Doing this will make sure your chat history and outputs are neatly logged as part of your tracing data. If you’d like, I’m here to walk you through the implementation in your setup—just let me know!
b
how cna i change the name of this trace ?
t
Thanks for reaching out! Could you please help me understand a bit more about what you mean by changing the name of a trace? Are you talking about: • Renaming a telemetry trace or span in OpenTelemetry integration? • Renaming a participant or track in a LiveKit room? If it’s related to telemetry, could you let me know if you’re using the Agents framework and which language SDK you’re working with (like Python or Node.js)? That way, I can guide you more precisely and make sure we get this sorted out together. Sources: Logs, metrics, and telemetry | LiveKit Docs | livekit-rtc/livekit/rtc/_proto/room_pb2.pyi | Runtime/Scripts/Participant.cs | livekit-ffi/src/livekit.proto.rs | tracer/tracer.go
b
which field to apss so that i do not have to see it as agent_session i can name it anything ?
t
I'm happy to help you customize the name of a trace instead of sticking with the default "agent_session." You can easily set the name of the span explicitly when you start it. In OpenTelemetry, spans are like individual units of work, and you have the flexibility to give them a custom name right when you create them. Here’s a friendly example in Python to guide you:
Copy code
from opentelemetry import trace
from opentelemetry.trace import TracerProvider
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor, ConsoleSpanExporter

# Set up the tracer provider
provider = TracerProvider()
trace.set_tracer_provider(provider)
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))

# Get a tracer instance
tracer = trace.get_tracer(__name__)

# Start a span with a custom name
with tracer.start_as_current_span("custom_trace_name") as span:
    span.set_attribute("key", "value")
    # Add your logic here
Just swap out "custom_trace_name" with whatever name you'd like your trace to have. This way, your trace will show up with your chosen name in the tracing backend, making it easier to identify. If you have any questions or if you’re working with a different language or framework, please don’t hesitate to ask. I’m here to help you every step of the way!