I am startign to implment the livekit agent-starte...
# ask-ai
d
I am startign to implment the livekit agent-starter-swiftui repo in xcode and want it to work properly. Now the ios swift client successfully connects to the room, but i get the error that the “agent did not connect to the room” . I have a livekit token server up and running, and have set the token server id in my xcode project settings
t
Thanks for reaching out, Pär. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! That pesky error "agent did not connect to the room" usually pops up when the AI agent can’t join the room within the expected time. No worries though—here are some friendly steps to help you sort it out: 1. Double-check Your Server-Side Agent Setup: Make sure your backend server is set up right to send the AI agent when the room is created. This usually involves explicitly dispatching the agent during token generation. 2. Review Your Token Generation Logic: Take a peek at your token service to confirm it includes the agent dispatch configuration and that the tokens have the right permissions. 3. Validate Your Webhook Endpoint: If you’re using webhook-based dispatch, ensure your webhook endpoint is properly exposed and secure. It’s also a good idea to test it on its own to make sure it’s working smoothly. 4. Keep an Eye on Room Connection State: Use the
room.connectionState
property to check that the room is fully connected before looking for the agent. 5. Consider Tweaking the Timeout: If the agent is taking a bit longer to connect, try increasing the timeout duration temporarily just to see if that helps. 6. Turn on Debug Logging: Enable verbose logging in the LiveKit SDK so you can follow what’s happening in real-time and spot where things might be going sideways. Starting with the server-side agent dispatch is usually your best bet since that’s often where things get stuck. If you want a hand with setting up explicit agent dispatch or checking your webhook delivery, just give me a shout—I’m here to help! Sources: Shared/StreamerPrepareView.swift | complex-agents/drive-thru/frontend/components/app.tsx | VoiceAgent/App/AppViewModel.swift | Web and mobile frontends | LiveKit Docs | components/provider.tsx
d
how can i turn on Turn on Debug Logging
t
Hey there! If you want to enable debug logging in the LiveKit iOS Swift SDK, here’s a simple way to do it: 1. General SDK Logging: Just add this code to your app to turn on debug-level logging for the whole SDK: `import Logging LoggingSystem.bootstrap { var logHandler = StreamLogHandler.standardOutput(label: $0) logHandler.logLevel = .debug return logHandler }`Pop this into your app’s initialization code, like in
AppDelegate
or an
AppContext
initializer. 2. Another Easy Way: You can also use LiveKit’s built-in method to set up logging:
LiveKitSDK.setLoggerStandardOutput()
3. For Broadcast Extensions: If you’re using
LKSampleHandler
for screen sharing, just override the
enableLogging
property like this:
Copy code
import LiveKit

@available(macCatalyst 13.1, *)
class SampleHandler: LKSampleHandler {
    override var enableLogging: Bool { true }
    override var verboseLogging: Bool { true }
}
You’ll see the logs right in Xcode’s console while debugging, or in the device’s logging system when you’re running on a real device. If you hit any bumps or want to chat more about this, just let me know—I’m here to help! Sources: Sources/LiveKit/Broadcast/LKSampleHandler.swift | iOS/BroadcastExt/SampleHandler.swift | example/ios/LivekitReactNativeExample.xcodeproj/project.pbxproj | uikit-minimal/uikit-minimal/AppDelegate.swift | Shared/Contexts/AppContext.swift
d
where would i place LiveKitSDK.setLoggerStandardOutput() in my code?
t
Hey there! To make sure logging kicks in right away, it's best to place
LiveKitSDK.setLoggerStandardOutput()
early in your app's lifecycle. Here are a couple of spots where it fits nicely: 1. In the AppDelegate: If you’re using an
AppDelegate
, just pop it into the
application(_:didFinishLaunchingWithOptions:)
method like this:
Copy code
import UIKit
import LiveKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        LiveKitSDK.setLoggerStandardOutput()
        return true
    }
}
2. In the SwiftUI App Struct: If your app is built with SwiftUI, you can add it inside the
init
method of your
@main
app struct:
Copy code
import SwiftUI
import LiveKit

@main
struct YourApp: App {
    init() {
        LiveKitSDK.setLoggerStandardOutput()
    }

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
If anything’s unclear or you want a hand with this, just give me a shout—I’m here to help!
d
is this correct?
Copy code
import LiveKit
import SwiftUI

@main
struct VoiceAgentApp: App {
    
    //Livekit extra logging, optional.
    LiveKitSDK.setLoggerStandardOutput()
    
    // Create the root view model
    private let viewModel = AppViewModel()

    var body: some Scene {
        WindowGroup {
            AppView()
                .environment(viewModel)
        }
        #if os(visionOS)
        .windowStyle(.plain)
        .windowResizability(.contentMinSize)
        .defaultSize(width: 1500, height: 500)
        #endif
    }
}
t
Hey there! It looks like there’s a tiny hiccup with where your code is placed. The
LiveKitSDK.setLoggerStandardOutput()
call works best inside the
init
method of your
VoiceAgentApp
struct. This way, it runs right when your app starts up. Here’s how you can fix it:
Copy code
import LiveKit
import SwiftUI

@main
struct VoiceAgentApp: App {
    
    // Create the root view model
    private let viewModel = AppViewModel()

    init() {
        // LiveKit extra logging, optional.
        LiveKitSDK.setLoggerStandardOutput()
    }

    var body: some Scene {
        WindowGroup {
            AppView()
                .environment(viewModel)
        }
        #if os(visionOS)
        .windowStyle(.plain)
        .windowResizability(.contentMinSize)
        .defaultSize(width: 1500, height: 500)
        #endif
    }
}
If anything’s unclear or you want to dive deeper, just give me a shout—I’m here to help!
d
another question: how does the livekit IOS agent starter repo know which of my agents it should connect to? my agent is runing in the livekit agent cloud
t
Hey there! Could you help me understand how your agent is set up in the LiveKit Agent Cloud? For example: • Are you using a specific agent ID or name? • Is the agent being dispatched through a webhook or assigned directly when generating the token? • Do you have any particular settings in your token server for choosing the agent? Knowing this will help me give you the best possible support. Thanks!
d
i have setup a specific agent in the livekit cloud. it is on this url (i have rephrased the aeaxct id for security) https://cloud.livekit.io/projects/p_3vqw6qvms62/agents/CA_Xp5pkkFUV99p I am not sure if the ios code is using exactly that
t
Hey there! Just a quick heads-up: the iOS agent starter repo doesn’t pick which agent to connect to on its own. Instead, that’s handled by the server-side setup and dispatch logic. To make sure your specific agent gets used, here’s a simple step-by-step guide: 1. Name Your Agent: When you deploy your agent in the LiveKit Agent Cloud, give it a unique name by setting the
agent_name
field in your backend configuration. For example, in Python:
WorkerOptions(agent_name="my-ios-agent")
2. Deploy with a Known ID: Use the LiveKit CLI to deploy your agent with a specific ID like this:
lk agent deploy --id my-ios-agent
3. Configure Dispatch Logic: Make sure your token server or room creation logic includes dispatch rules that target your named agent. For example, include the agent name when generating tokens. 4. Verify Agent Identity: You can double-check your agent’s name and ID in the LiveKit dashboard or by running:
lk agent list
5. iOS App Behavior: Your iOS app connects to the room, and the server-side dispatch takes care of getting the right agent to join. There’s no need to configure the agent selection on the iOS client side. If you want, I’m here to help you verify your agent’s setup or walk you through setting up those dispatch rules. Just let me know! Sources: templates.yaml | cmd/lk/agent.go | VoiceAgent/VoiceAgent.xcconfig | Swift quickstart | LiveKit Docs | examples/other/translation/multi-user-translator.py
d
wher can i see the log outputs form LiveKitSDK.setLoggerStandardOutput()?
t
Hey there! Just a quick heads-up about where you can find the log outputs from `LiveKitSDK.setLoggerStandardOutput()`: 1. Xcode Console: When you’re running your app in Xcode, the logs will show up right in the Xcode debug console. Just make sure the console is open by going to View > Debug Area > Activate Console. 2. Device Logs: If you’re testing on a physical device, you can check out the logs using the macOS Console app. Just connect your device, pick it in the Console app, and filter to see your app’s logs. 3. Standard Output: Running your app in a terminal or somewhere that captures standard output? The logs will pop up there too. If you want a hand finding or understanding these logs, just give me a shout—I’m here to help!