Can I keep any user defined variable attached to t...
# ask-ai
n
Can I keep any user defined variable attached to the job context?
t
Thanks for reaching out, Tareq. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! You can attach user-defined variables to the job context in LiveKit in a few different ways: • Job Metadata: You can pass data when you dispatch a job using the
metadata
field, which is great for holding structured info like JSON. • Participant and Room Metadata/Attributes: This lets you store and access user-defined data that's tied dynamically to participants or rooms. • AgentSession Userdata: Use the
userdata
parameter in
AgentSession
if you want strongly-typed, mutable state that sticks around across sessions or handoffs. • Process-Level User Data: For reusable components at the worker process level, you can use the
JobProcess.userdata
dictionary. • Custom Logging Context: If you want to add custom fields to your logs for easier debugging and monitoring,
ctx.log_context_fields
is the way to go. These options give you plenty of flexibility to manage your user-defined variables within a job context. If you'd like, I’m happy to share more details or examples for any of these—just let me know! Sources: Workflows | LiveKit Docs | flows/declarative_flow.py | Job lifecycle | LiveKit Docs | examples/voice_agents/multi_agent.py
n
tell me about log context fields
t
Hey there! Just wanted to share a quick tip about log context fields in LiveKit—they let you add custom info to your logs, making debugging and monitoring way easier. Here’s the scoop: • Python SDK (Agents): You can use the
log_context_fields
property on the
JobContext
object to add your own fields to every log entry. Here’s a simple example:
Copy code
async def entrypoint(ctx: JobContext):
    ctx.log_context_fields = {
        "worker_id": ctx.worker_id,
        "room_name": ctx.room.name,
    }
• JavaScript SDK: Try out the
setLogExtension
function to dynamically tack on extra context to your logs. Like this:
Copy code
setLogExtension((level, msg, context) => {
  const enhancedContext = { ...context, timeStamp: Date.now() };
  // Forward enhancedContext to your logging provider
});
With these, you can easily include things like user IDs, room names, or timestamps in your logs. If you want more examples or need help with a specific SDK or scenario, just give me a shout—I’m here to help! Sources: examples/voice_agents/basic_agent.py | packages/livekit-rtc/src/proto/ffi_pb.ts | Sources/LiveKit/Extensions/Logger.swift | Runtime/Scripts/Proto/Ffi.cs | Job lifecycle | LiveKit Docs