Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.egisai.co/llms.txt

Use this file to discover all available pages before exploring further.

egisai.set_context(
    *,
    user_id: str | None = None,
    user_role: str | None = None,
    agent: str | None = None,
    agent_id: str | None = None,
    session_id: str | None = None,
    workflow_id: str | None = None,
) -> None
Attach per-request metadata to every governed call that runs inside the same async task or thread. The fields you set land on each audit event and on the context that the policy engine sees. set_context() writes to a ContextVar, so async tasks and child threads inherit the value cleanly without leaking across requests.

When to use it

Use set_context() for:
  • Multi-tenant attribution — pass the end-customer’s user_id and session_id per request so each audit event identifies who triggered it.
  • Sub-agents — name a logical sub-agent (agent="billing-bot") so calls from this section of code show up as a distinct Agent on the dashboard.
  • Workflows — group related calls with a workflow_id.
If you only have one Agent per process, you don’t need to call this — the app= argument on egisai.init() is enough.

Parameters

user_id
str | None
End-customer or end-user identifier for multi-tenant attribution. Recorded on each audit event for the lifetime of the context.
user_role
str | None
Optional role label for the user (e.g. "admin", "customer"). Free-form.
agent
str | None
Logical sub-agent name. The first time the SDK sees a new name it registers it as an Agent on the platform; subsequent uses are cache hits.Wins over the process-level app from egisai.init() and over auto-detected identity for the duration of the context.
agent_id
str | None
Direct UUID escape hatch for callers that already know the platform’s internal Agent ID and don’t want a name-resolution round-trip.Most users should prefer agent= and let the SDK resolve the ID.
session_id
str | None
End-user session identifier. Useful for grouping conversation turns on the dashboard.
workflow_id
str | None
Identifier for an orchestration step or batch job. Lets you correlate related calls on the dashboard.
Passing None for any field leaves the existing value alone. To clear a field, set it to an empty string.

Returns

None. The active context is updated in place.

Examples

Multi-tenant FastAPI handler

from fastapi import FastAPI, Request
import egisai
import openai

egisai.init(app="orchestrator", env="production")
app = FastAPI()
client = openai.OpenAI()

@app.post("/chat")
async def chat(request: Request, body: dict):
    egisai.set_context(
        user_id=body["user_id"],
        session_id=body["session_id"],
        agent="customer-support-bot",
    )
    response = client.chat.completions.create(
        model="gpt-4.1",
        messages=body["messages"],
    )
    return response.choices[0].message.content
Each request lands on the dashboard tagged with the right user, session, and sub-agent.

Multiple sub-agents in one process

egisai.init(app="orchestrator", env="production")

def triage(query):
    egisai.set_context(agent="triage-agent")
    return client.chat.completions.create(...)

def billing(query):
    egisai.set_context(agent="billing-agent")
    return client.chat.completions.create(...)
Calls from each function show up as separate Agents on the dashboard. See Multi-agent context for more patterns.

Workflow correlation

import uuid
import egisai

workflow_id = uuid.uuid4().hex
egisai.set_context(workflow_id=workflow_id)
for step in plan:
    run_step(step)         # all calls share workflow_id

Resolution precedence

When attributing a call, the SDK uses:
  1. set_context(agent_id=...) (explicit UUID).
  2. set_context(agent=...) (resolved name).
  3. System-prompt fingerprint (auto-detected sub-agent).
  4. The process-level app= from egisai.init().
Explicit set_context calls always win.

What’s next

Multi-agent context

Patterns for several sub-agents in one process.

Agents

The mental model behind agent identity.