Skip to content

Healthcare AI Context

Healthcare AI Context

The Klira SDK provides healthcare-specific context functions that attach patient, clinical, and interaction metadata to your trace spans. This enables compliance auditing, clinical workflow tracking, and patient safety monitoring.

Setting Patient Context

Use set_patient_context() to associate a patient with the current trace span. This enables per-patient audit trails and compliance tracking.

from klira.sdk.healthcare import set_patient_context
set_patient_context(
patient_id="patient_12345",
encounter_id="enc_67890"
)

Parameters

ParameterTypeRequiredDescription
patient_idstrYesUnique patient identifier
encounter_idstrNoClinical encounter or visit identifier

Span Attributes Set

  • klira.healthcare.patient_id
  • klira.healthcare.encounter_id (when provided)

Setting Clinical Context

Use set_clinical_context() to add clinical metadata such as department, specialty, or clinical domain to the current span.

from klira.sdk.healthcare import set_clinical_context
set_clinical_context(
department="Emergency",
specialty="Cardiology",
clinical_domain="cardiac_triage"
)

Parameters

ParameterTypeRequiredDescription
departmentstrNoHospital department or unit
specialtystrNoMedical specialty
clinical_domainstrNoClinical domain for policy routing

Span Attributes Set

  • klira.healthcare.department
  • klira.healthcare.specialty
  • klira.healthcare.clinical_domain

Setting Interaction Modality

Use set_interaction_modality() to record how the patient or clinician is interacting with the AI system.

from klira.sdk.healthcare import set_interaction_modality
set_interaction_modality("chat")

Supported Modalities

ValueDescription
"chat"Text-based chat interface
"voice"Voice or speech interface
"ehr_embedded"Embedded within an EHR system
"api"Direct API integration

Complete Working Example

from klira.sdk import Klira
from klira.sdk.decorators import workflow, agent
from klira.sdk.healthcare import (
set_patient_context,
set_clinical_context,
set_interaction_modality,
)
Klira.init(
app_name="ClinicalAssistant",
api_key="klira_live_your_key",
clinical_domain="general_medicine",
)
@workflow(name="patient_consultation", user_id="dr_smith", conversation_id="consult_001")
async def patient_consultation(patient_id: str, question: str) -> str:
# Set healthcare context on the current span
set_patient_context(patient_id=patient_id, encounter_id="enc_2026_001")
set_clinical_context(department="Internal Medicine", specialty="Gastroenterology")
set_interaction_modality("ehr_embedded")
# Your clinical AI logic here
response = await clinical_llm_call(question)
return response
@agent(name="clinical_agent")
async def clinical_llm_call(question: str) -> str:
# The healthcare context set above is inherited by child spans
# Guardrails can use clinical_domain for domain-specific policies
return "Clinical response based on guidelines..."

Integration with Decorators

Healthcare context functions work alongside Klira decorators. Context set within a @workflow or @agent function is automatically attached to the active span and inherited by child spans.

You can also set clinical_domain at init time to apply it globally:

Klira.init(
app_name="ClinicalApp",
api_key="klira_live_your_key",
clinical_domain="oncology",
)

This global clinical_domain is used by guardrails to select domain-specific policies when no per-span override is set.