rapid-honey-43838
03/03/2026, 8:00 AMtall-airline-16756
03/03/2026, 8:00 AMtall-airline-16756
03/03/2026, 8:01 AMrapid-honey-43838
03/03/2026, 8:10 AMtall-airline-16756
03/03/2026, 8:10 AMrapid-honey-43838
03/03/2026, 8:12 AMtall-airline-16756
03/03/2026, 8:12 AMrapid-honey-43838
03/03/2026, 8:12 AMtall-airline-16756
03/03/2026, 8:12 AMrapid-honey-43838
03/03/2026, 8:53 AMtall-airline-16756
03/03/2026, 8:53 AMcontext_variables = {"name": "Shayne", "age": 35, "city": "Toronto"}
instructions = f"""
You are a helpful agent. The user's name is {context_variables['name']}.
They are {context_variables['age']} years old and live in {context_variables['city']}.
"""
agent = ContextAgent(context_vars=context_variables)
await session.start(agent=agent, room=ctx.room)
2) Agent Builder variable placeholders (no-code)
• In Agent Builder prompts use the {{metadata.variable_name}} syntax.
• When you dispatch the agent, pass metadata like {"user_name":"Alice","city":"Seattle"} and the builder will render those placeholders automatically.
Example prompt in Builder: You are assisting {{metadata.user_name}} from {{metadata.city}}.
3) Webhook / runtime injection for live values
• For values that change during a call (e.g., live metrics, external API results), fetch the data at runtime and update the session instructions or call a function/tool that the agent can use.
• Use server-side code to fetch the external value, then call the agent with updated context or invoke a function tool exposing that data.
4) session.userdata for multi-turn state
• Store conversation state or user preferences in session.userdata so later turns can reference/update those variables.
• Use session.userdata to keep personalization across turns without re-sending all metadata each time.
Example pattern:
state = context.session.userdata
state.user_name = "Shayne"
# Later:
instructions = f"Welcome back, {state.user_name}. How can I help today?"
5) Function tools / RunContext for structured data
• If your agent uses tools/functions, pass dynamic data into those tools via a RunContext so the agent can call them and receive structured responses to incorporate into replies.
When to use which:
• Use job metadata or Agent Builder placeholders for initial personalization and static per‑session data.
• Use webhooks/runtime injection or function tools for data that changes frequently or must be fetched live.
• Use session.userdata to preserve state across multiple turns or sessions.
Security and formatting tips:
• Sanitize and validate variables before injecting into prompts to avoid prompt injection or malformed prompts.
• Prefer templating (f-strings or .format) on the server side so placeholders are resolved before sending instructions.
• For large or sensitive data, pass a reference (ID) and fetch it securely at runtime rather than embedding raw secrets in prompts.
If you tell me:
• Are you using the SDK (which language: Node, Go, Python) or Agent Builder?
• Are the variables static at session start or do they need live updates during the session?
I’ll give a concrete, runnable example in your language (including secure templating and a sample webhook/function integration) tailored to your exact workflow.
Sources: Context Variables | LiveKit Documentation | Agent Builder | LiveKit Documentation | Survey Calling Agent | LiveKit Documentation | Building multi-agent architectures with LiveKit agents | LiveKit