what is the recommended way from livekit to store ...
# ask-ai
g
what is the recommended way from livekit to store state like e.g. information from an api for later processing
t
Thanks for reaching out, Christian. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To store state like API information for later processing in LiveKit, here are some handy approaches depending on your platform and what you need: • iOS/macOS: Go for Keychain to keep sensitive info like API keys and tokens safe and sound. • Web: Try combining local storage with a state management library like Zustand for a smooth and persistent way to handle your state. • LiveKit Participant Attributes: Use participant attributes or metadata fields when you want to sync application-specific state across participants. • Environment Variables: Keep sensitive info like API keys in environment variables so you don’t have to hardcode them in your app. • Flutter: SharedPreferences is a great choice for simple, persistent storage of small bits of data. Pick the method that feels right for your platform and what your app needs. If you want, I’m here to help with more details on any of these! Sources: Next.js 13+ quickstart | LiveKit Docs | example/lib/main.dart | Multiplatform/Support/SecureStore.swift | agent-console/hooks/use-room-info.tsx | Participant attributes and metadata | LiveKit Docs
g
i meant inside an agent
t
To keep state inside an agent in LiveKit, a great way to do it is by using the userdata attribute of the AgentSession. It’s pretty flexible and you can shape it using a dataclass to organize your state neatly. Here’s a quick example:
Copy code
from dataclasses import dataclass

@dataclass
class MySessionInfo:
    user_name: str | None = None
    age: int | None = None

from livekit.agents import AgentSession

session = AgentSession[MySessionInfo](
    userdata=MySessionInfo(),
    # ... other parameters like tts, stt, llm, etc.
)
With this setup, the userdata is available throughout your agent’s workflow as session.userdata, and you can also access it inside function tools via the RunContext. If you’re dealing with more complex state management, another option is to use participant attributes or metadata. These get stored and synced by the LiveKit server and work well for sharing smaller pieces of state across participants. Feel free to ask if you want me to walk you through more examples or details tailored to your project!