red-accountant-48215
08/01/2025, 3:53 PMtall-airline-16756
08/01/2025, 3:53 PMtall-airline-16756
08/01/2025, 3:54 PMred-accountant-48215
08/01/2025, 3:54 PMtall-airline-16756
08/01/2025, 3:55 PMred-accountant-48215
08/01/2025, 3:56 PMtall-airline-16756
08/01/2025, 3:56 PMmetrics_collected event on your LLM instance.
3. Finally, create a callback function to handle the metrics, including TTFT.
from livekit.agents.metrics import LLMMetrics
from livekit.plugins.openai import LLM
# Initialize the OpenAI LLM
llm = LLM(model="gpt-4")
# Define a callback function to handle metrics
def on_metrics_collected(metrics):
ttft = metrics.ttft # Access the Time To First Token
print(f"TTFT: {ttft:.4f} seconds")
# Attach the event listener
llm.on("metrics_collected", on_metrics_collected)
# Use the LLM to generate completions and trigger metrics collection
async def generate_completion(prompt):
async with llm.chat(chat_ctx=prompt) as response:
pass
# Start generating completions to collect metrics
generate_completion("Hello, how are you?")
For JavaScript:
1. Initialize the OpenAI LLM using the JavaScript client.
2. Subscribe to the metrics_collected event on your LLM instance.
3. Handle the metrics inside the event callback.
const { OpenAILLM } = require("livekit-plugins");
// Initialize the OpenAI LLM
const llm = new OpenAILLM({
model: "gpt-4",
});
// Define an event listener for metrics
llm.on("metrics_collected", (metrics) => {
const ttft = metrics.ttft; // Access the Time To First Token
console.log(`TTFT: ${ttft.toFixed(4)} seconds`);
});
// Use the LLM to generate completions and trigger metrics collection
async function generateCompletion(prompt) {
const response = await llm.chat(prompt);
// Process the response
}
// Start generating completions to collect metrics
generateCompletion("Hello, how are you?");
And that’s it! These steps will help you keep an eye on TTFT and other useful metrics. If you have any questions or want a hand with anything else, just let me know—I’m here to help!red-accountant-48215
08/01/2025, 3:57 PMtall-airline-16756
08/01/2025, 3:58 PMred-accountant-48215
08/01/2025, 3:59 PMtall-airline-16756
08/01/2025, 4:00 PMregion parameter when you initialize the LLM class. Here’s an example:
from livekit.plugins.aws import LLM
llm = LLM(
model="anthropic.claude-3-5-sonnet-20240620-v1:0",
region="us-west-2" # This sets your AWS region
)
• For Azure OpenAI: Use the azure_endpoint parameter inside the with_azure method. Here’s how:red-accountant-48215
08/01/2025, 4:11 PMtall-airline-16756
08/01/2025, 4:11 PMbase_url parameter in the LLM configuration. Here’s a quick example to help you out:
from livekit.plugins.openai import LLM
openai_llm = LLM(
model="gpt-4",
base_url="<YOUR_REGION_SPECIFIC_ENDPOINT>" # Just swap this with your region-specific endpoint
)
Right now, OpenAI doesn’t have detailed docs on region-specific endpoints, so if you’re unsure, it’s a good idea to check in with OpenAI support. And of course, if you have any questions or need a hand, just give me a shout!red-accountant-48215
08/01/2025, 4:23 PMtall-airline-16756
08/01/2025, 4:23 PMAPITimeoutError you're seeing in your OpenAI LLM integration, you can tweak the httpx.Timeout settings a bit. Here's a setup that tends to work well:
from httpx import Timeout
# Recommended timeout configuration
timeout = Timeout(connect=30.0, read=10.0, write=10.0, pool=10.0)
# Pass this timeout to your LLM initialization
llm = OpenAILLM(
model="gpt-4",
timeout=timeout
)
What’s going on here:
• Connect Timeout: Bumped up to 30 seconds so there’s plenty of time to establish a connection.
• Read/Write Timeout: Set to 10 seconds to give enough time for data to flow smoothly.
• Pool Timeout: Also 10 seconds, allowing extra time for connection pooling tasks.
This setup gives you a bit more breathing room with timeouts, but still keeps things running efficiently. If you want to chat more about this or need a hand with anything else, just let me know—I’m here to help!