How to hande llm API call error when using azure o...
# ask-ai
l
How to hande llm API call error when using azure openai
t
Thanks for reaching out, Rajadurai. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to handle LLM API call errors when using Azure OpenAI. Below are concise, actionable options you can use with LiveKit Agents to make your agent resilient and user-friendly. 1) Use FallbackAdapter for automatic failover • Chain providers so requests automatically retry on the next provider when Azure fails. • Example (Python):
Copy code
from livekit.agents import llm
from livekit.plugins import openai

session = AgentSession(
  llm=llm.FallbackAdapter([
    openai.LLM(model="gpt-4o"),                        # primary
    openai.LLM.with_azure(model="gpt-4o", ...),        # fallback
  ]),
)
• Failed providers are marked unhealthy and periodically rechecked. 2) Listen for ErrorEvent and handle errors programmatically • Subscribe to the session
error
event to log, notify users, or trigger recovery. • Example (Python):
Copy code
@session.on("error")
def on_error(event):
    if not event.error.recoverable:
        # Fatal error (e.g., auth failure)
        print(f"Unrecoverable error: {event.error}")
        session.say("Sorry, I'm having trouble processing your request.")
    else:
        # Transient error (e.g., rate limit)
        print(f"Transient error: {event.error}")
• Useful properties:
error.recoverable
,
source
,
model_config
. 3) Ensure authentication and environment variables are correct • Set Azure keys/endpoints and API version:
Copy code
AZURE_OPENAI_API_KEY=your-key
AZURE_OPENAI_ENDPOINT=<https://your-endpoint.openai.azure.com>
OPENAI_API_VERSION=2024-10-01-preview
• You can also use a Microsoft Entra ID token if needed. Quick best-practice checklist • Chain providers with
FallbackAdapter
for high availability. • Subscribe to
error
events and distinguish recoverable vs unrecoverable errors. • Inform users gracefully (e.g.,
session.say()
). • Validate API keys, endpoint, and API version early. If you want, I can: • Show a more complete code example for your language (Python or JS). • Help design retry/backoff logic or logging strategies tailored to your app. Sources: Events and error handling | LiveKit docs | Azure OpenAI LLM plugin guide | LiveKit docs