Is it possible to list the Agents from the Python ...
# ask-ai
b
Is it possible to list the Agents from the Python SDK? I know is possible via CLI but looking for a Python option
t
Thanks for reaching out, Carlos. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — you're asking whether the Python SDK can list Agents (you know the CLI can). Good question. Short answer: The Python SDK does not include a documented method to list Agents. The SDKs and agent framework focus on building/running agents and on room/ingress/egress functionality, while agent listing is currently done via the LiveKit CLI (
lk agent list
). What you can do next • Use the CLI: continue using
lk agent list
for an official way to enumerate deployed agents. • If you need a programmatic approach now: would you be able to run the CLI from Python and parse its output as a temporary workaround? If so, I can show a small example of invoking
lk agent list
from Python and parsing JSON/text output. • If you want a native API/SDK method: I can help you draft a concise feature request describing your use case (e.g., list agents programmatically from Python to automate monitoring or deployment workflows). Which option would you prefer — a Python example that calls the CLI, or help preparing a feature request for native SDK support? Sources: Examples | livekit/livekit_composite | DeepWiki | Python SDKs | livekit/livekit_composite | DeepWiki | Agents (Python) | livekit/livekit_composite | DeepWiki
b
Python example that calls the CLI
t
Thanks — I'll show a simple, practical Python example that runs the LiveKit CLI (
lk agent list
) and parses its output. I’ll include two variants: one that expects JSON output (preferred) and one that parses plain text output if JSON isn't available. Before you run this: • Ensure the
lk
CLI is installed and authenticated in the environment where your Python code will run. • Confirm
lk agent list
supports a JSON output flag (if it does, prefer that) — if not, use the text-parsing example below. 1) Preferred: run CLI with JSON output (if supported) • This runs the CLI asking for JSON output and parses it into Python objects.
Copy code
import subprocess
import json

def list_agents_json():
    # If the `lk` CLI supports a JSON flag like `--output json` or `-o json`, use it.
    # Replace the flag below with the correct one if different.
    cmd = ["lk", "agent", "list", "--output", "json"]

    try:
        completed = subprocess.run(cmd, capture_output=True, text=True, check=True)
        data = json.loads(completed.stdout)
        # `data` will be a dict/list depending on CLI output structure.
        return data
    except subprocess.CalledProcessError as e:
        print("CLI returned non-zero exit:", e.returncode)
        print("stderr:", e.stderr)
        raise
    except json.JSONDecodeError:
        print("Failed to parse JSON output:")
        print(completed.stdout)
        raise

if __name__ == "__main__":
    agents = list_agents_json()
    print("Agents:", json.dumps(agents, indent=2))
2) Fallback: parse plain-text CLI output • Use this if the CLI only returns human-readable text. Adjust parsing to match the exact text format returned by
lk agent list
.
Copy code
import subprocess

def list_agents_text():
    cmd = ["lk", "agent", "list"]
    completed = subprocess.run(cmd, capture_output=True, text=True, check=True)
    out = completed.stdout.strip().splitlines()

    agents = []
    # Example parsing strategy:
    # - If the CLI prints a header row and columns, skip the header and split columns.
    # - If it prints one agent per line, parse each line accordingly.
    # Adjust the logic below to match actual CLI formatting.

    # Example: header like "NAME   STATUS   VERSION"
    if not out:
        return agents

    # detect and skip header if present
    header_idx = 0
    if any(h.lower().startswith("name") or "status" in h.lower() for h in out[0].split()):
        header_idx = 1

    for line in out[header_idx:]:
        # simple split by whitespace (may need more robust parsing)
        parts = line.split()
        if not parts:
            continue
        # Example mapping; update indices to match real output
        agent = {
            "name": parts[0],
            "status": parts[1] if len(parts) > 1 else None,
            "version": parts[2] if len(parts) > 2 else None,
        }
        agents.append(agent)

    return agents

if __name__ == "__main__":
    agents = list_agents_text()
    for a in agents:
        print(a)
Notes and tips • Prefer the JSON output option if the CLI supports it; it's more reliable than parsing human-readable text. • If running this in production, handle errors and timeouts, and consider using
subprocess.run(..., timeout=30)
. • If you need help adapting the text parser to the exact
lk agent list
output, paste a sanitized example of the CLI output and I’ll tailor the parsing logic.