tell me all about `get_job_context()` function
# ask-ai
r
tell me all about
get_job_context()
function
t
Thanks for reaching out, Jeff. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The
get_job_context()
function is a utility in the LiveKit Agents framework that provides access to the current job's context and environment. It uses Python's
contextvars
module to store and retrieve the job context. Here’s how it works:
Copy code
_JobContextVar = contextvars.ContextVar["JobContext"]("agents_job_context")

def get_job_context() -> JobContext:
    ctx = _JobContextVar.get(None)
    if ctx is None:
        raise RuntimeError(
            "no job context found, are you running this code inside a job entrypoint?"
        )
    return ctx
The function retrieves the
JobContext
from a context variable. If no context is found (i.e., the code is not running inside a job entrypoint), it raises a
RuntimeError
with a descriptive message. What the
JobContext
provides:
Room interface: Access to the LiveKit room (
ctx.room
). • API client: LiveKit API for making server calls (
ctx.api
). • Job information: Details about the current job (
ctx.job
). • Worker ID: Identifier of the worker (
ctx.worker_id
). • Process information: The job process (
ctx.proc
). Common usage patterns: 1. *End calls by deleting rooms:*`job_ctx = get_job_context() await job_ctx.api.room.delete_room( api.DeleteRoomRequest(room=job_ctx.room.name) )` 2. *Access room information for SIP operations:*`job_ctx = get_job_context() await job_ctx.api.sip.transfer_sip_participant(...)` 3. *Get HTTP session configuration:*`try: http_proxy = get_job_context().proc.http_proxy except RuntimeError: http_proxy = None` Additionally, the function has an alias
get_current_job_context
that works identically. Sources: livekit-agents/livekit/agents/utils/http_context.py | agent.py