Hey there. I'm trying to load some system prompts ...
# ask-ai
a
Hey there. I'm trying to load some system prompts from my DB into memory at worker initialization - i want to use them across different jobs but only load them once. how do i go about this?
t
Thanks for reaching out, Isaam. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
✅ 1
Hey there! If you want to load system prompts from your database into memory when your worker starts up and use them across different jobs, here’s a friendly guide to help you out: 1. First, define the system prompt as a static string constant so it can be easily shared across clients. 2. Next, tokenize that system prompt just once during initialization to get it ready for use. 3. Then, evaluate the tokenized prompt and cache the result for quick access. 4. Finally, share this cached prompt across multiple sequences or clients to avoid doing the same work over and over. This way, you’re loading and processing the system prompt just once, which really helps boost performance when things get busy. If you want me to walk you through it with more details or examples, just let me know—I’m here to help! Sources: tools/tts/tts.cpp | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | Vision Agent Quickstart | LiveKit Docs
a
there's not on system prompt, theres a bunch. we use this function in every single job:
Copy code
async def get_system_instructions():
    systemprompts = client.get_db_collection("systemprompts")
    systemprompt = systemprompts.find_one({})
    return systemprompt.get("ivr", ""), systemprompt.get("vm", ""), systemprompt.get("cal", ""), systemprompt.get("transfer", ""), systemprompt.get("end", ""), systemprompt.get("human", "")
in this case how do i ensure that my prompts are loaded into memory and ready on worker intialization to be used in different files for all inccoming jobs
t
Hey there! If you want to preload multiple prompts into memory right when the worker starts and have them handy for all jobs, you can use the
prewarm
function in the
WorkerOptions
. This function runs just once when the worker process kicks off, letting you load data into shared memory via
proc.userdata
. Then, all jobs running in the same process can easily access it. Here’s a simple way to do it: 1. Define a
prewarm
function:
Grab all your system prompts from the database and stash them in
proc.userdata
. 2. Access the preloaded prompts: Inside your job’s
entrypoint
, pull the prompts from
ctx.proc.userdata
. 3. Register the functions: When starting the worker, pass both the
prewarm
and
entrypoint
functions to
WorkerOptions
. Here’s an example to make it clearer:
Copy code
def prewarm(proc: JobProcess):
    # Load prompts from DB
    systemprompts = client.get_db_collection("systemprompts")
    proc.userdata["prompts"] = systemprompts.find_one({})

async def entrypoint(ctx: JobContext):
    # Access preloaded prompts
    prompts = ctx.proc.userdata["prompts"]
    ivr_prompt = prompts.get("ivr", "")
    # Use the prompts in your job logic

if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, prewarm_fnc=prewarm))
So, this way your prompts load just once at startup and are ready to go for every job that comes in. If you have any questions or want me to walk you through it, just let me know—I’m here to help! Sources: Worker options | LiveKit Docs | agent/main.py
a
but itsn the prewarm function:
Copy code
# a function to perform any necessary initialization in a new process.
? doesn't that mean it runs with every new job/process? so there's no point and essentially it would be doing the same as loading it everytime a call comes in, no? i want it such that this is loaded on the server already and used by every call without having to fetch it from the DB every single time!
t
Hey there! Just to clarify, the
prewarm
function in
WorkerOptions
runs only once per process, not every time a new job comes in. When your worker starts up, it spins up a pool of idle processes (based on the
num_idle_processes
setting), and the
prewarm
function runs in each of these during initialization. These prewarmed processes stick around and get reused for handling jobs. So, the prompts you load in
prewarm
stay in memory and are ready to go for all jobs handled by that same process. This way, you don’t have to fetch prompts from the database every single time a new job arrives — they’re loaded just once per process. If anything’s unclear or you want a hand with the implementation, just let me know! I’m here to help. Sources: Worker options | LiveKit Docs
a
so i have another question - i'm working with a client who uses vicidial. they're gonna route their campaigns calls through my livekit based platofrm. i will receive an incoming call from them and in their SIP INVITE headers they will give me specific things in their headers such as lead id and what not. when creating the sip inbound trunk should i dynamically allow them to map their headers to variables and then when creating their trunks mark it as such?
t
Handling custom SIP INVITE headers like lead ID for inbound trunks is totally doable with LiveKit! You’ve got a couple of ways to set up header mapping, so let me walk you through them: 1. Explicit Header Mapping: You can use the
headers_to_attributes
field to directly map specific SIP headers to participant attributes. For example:
Copy code
{
  "headers_to_attributes": {
    "X-Lead-ID": "leadId",
    "X-Campaign-ID": "campaignId"
  }
}
2. Automatic Header Mapping: Or, if you prefer, use the
include_headers
option to map headers automatically: ◦
SIP_NO_HEADERS
(0): Only maps headers you explicitly define ◦
SIP_X_HEADERS
(1): Maps all X-* headers to
sip.h.x-*
attributes ◦
SIP_ALL_HEADERS
(2): Maps all headers to
sip.h.*
attributes After you set this up, those headers will show up as participant attributes in your app. So, for instance, if you map
X-Lead-ID
to
leadId
, you can easily access it as a participant attribute. When you’re creating the inbound trunk, you can even let clients dynamically map their headers to variables using the
headers_to_attributes
field. This keeps things flexible and lets you capture exactly what they send in their SIP INVITE headers. If you want a hand with setting this up or have any questions, just let me know—I’m here to help! Sources: protobufs/rpc/io.proto | SIP APIs | LiveKit Docs | SIP participant | LiveKit Docs
a
hmm ive done explicit header mappings now tell me how i can use this in my livekit workflow how do i actually use the mapping created use the example of X-LEAD-ID: lead.id
t
Once you’ve mapped the
X-LEAD-ID
header to a participant attribute (like
lead_id
) using
headers_to_attributes
, you can easily use it in your LiveKit workflow like this: 1. Access the Attribute: The mapped header turns into a participant attribute. For example, in your application logic, you can grab it like this:
lead_id = participant.attributes.get("lead_id")
2. Use in Room Logic: When you’re managing participants in a LiveKit room, you can use that attribute to tailor behavior based on the lead ID. For example:
Copy code
if lead_id == "12345":
    # Do something special for this lead
    handle_lead_specific_logic()
3. Reverse Mapping (Optional): If you want to send the lead ID back in SIP headers (like in BYE or REFER requests), just set up
attributes_to_headers
like this:
Copy code
{
  "attributes_to_headers": {
    "lead_id": "X-LEAD-ID"
  }
}
This way, the lead ID stays handy throughout your workflow and can be used for routing, logging, or any other custom logic you need. If you have any questions or want to dive deeper, just let me know—I’m here to help! Sources: livekit/livekit_sip.pb.go | SIP APIs | LiveKit Docs | SIP APIs | LiveKit Docs
a
can i map attributes with dot notations for headers to attributes mapping such as "X-Lead-ID":"lead.id"? and if so how would i use it?
t
Hey there! The documentation doesn’t clearly say if dot notation (like
lead.id
) works in
headers_to_attributes
mappings. But here’s a little trick you can try: set up a trunk with a mapping like
{"X-Lead-ID": "lead.id"}
and see if the attribute shows up correctly on the participant. If dot notation does work, you’d grab the attribute in your app like this:
Copy code
lead_id = participant.attributes.get("lead.id")
If that doesn’t do the trick, you might want to stick with a simpler naming style, like
lead_id
. And hey, if you want me to walk you through it or have any other questions, just give me a shout—I’m here to help! Sources: SIP APIs | LiveKit Docs | livekit/livekit_sip.pb.go | SIP APIs | LiveKit Docs
a
can i cancel a CreateSIPParticipantRequest?
t
Hey there! The documentation doesn’t specifically mention a way to cancel a
CreateSIPParticipantRequest
once it’s started. If you’re hoping to cancel it programmatically, it might depend on how your app is handling the request (like through gRPC or HTTP). Could you let me know if you’re trying to cancel an ongoing request programmatically, or if you’re thinking of a different approach? That way, I can give you the best advice possible. Sources: Making outbound calls | LiveKit Docs | Making outbound calls | LiveKit Docs | livekit-api/livekit/api/sip_service.py | livekit/livekit_sip.pb.go
a
quick quesiton is this transfer TransferSIPParticipantRequest is this a SIP invite or a referrer based transfer
t
The
TransferSIPParticipantRequest
in LiveKit works through a SIP REFER-based transfer. The docs show that call transfers use the SIP REFER method, and you can easily add headers to the REFER SIP request using the
Headers
field in the request. If you want me to walk you through it or have any other questions, just let me know—I’m here to help! Sources: Transferring calls | LiveKit Docs | rpc/sip.pb.go
a
can we do an invite based transfer
t
It looks like the documentation doesn’t mention support for SIP INVITE-based transfers in LiveKit. Instead, transfers are done using the SIP REFER method through the
TransferSIPParticipantRequest
. If you’re working with a specific scenario that needs INVITE-based transfers, just let me know! I’d be happy to help explore other options or dig into more details with you.
a
if i wanna listen to DMTF tones and also use them as inputs from the user how do i do so? also i don't wanna send every number one by one because then the agent will immediately begin responding. how do i smartly do that? @room.on("sip_dtmf_received") def dtmf_received(dtmf: rtc.SipDTMF): logging.info(f"DTMF received from {dtmf.participant.identity}: {dtmf.code} / {dtmf.digit}")
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
a
wait do you not know how we can receive and use dmtf tones? its in the documentation lol: https://docs.livekit.io/sip/dtmf/
?
??\
Copy code
2025-09-11T03:01:37.738918756Z stdout F 2025-09-11 03:01:37,738 - ERROR voice-assistant - [Room -> web-68be49d4564e6a5813eca119-o7nns3ad1m] Error during entrypoint execution: a coroutine was expected, got None