hi, is it possible to reach the shell in livekit c...
# ask-ai
s
hi, is it possible to reach the shell in livekit cloud ?
t
Thanks for reaching out, Stefano. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You cannot access a shell on LiveKit Cloud. LiveKit Cloud is a fully-managed service, so LiveKit handles server operations and deployments (for example, agents are deployed with
lk agent create
) and customers do not get direct OS/container shell access. If you need to run custom code or troubleshoot, I can help with alternatives—e.g., deploying an agent, adding logging, or running diagnostic code inside your deployment. Which approach do you want to try? Sources: LiveKit Cloud | LiveKit Docs | Deploying to production | LiveKit Docs
s
so if i need to start a service on a local http port and reach it from my agent, how can i do it ?
t
You're asking how to run a local HTTP service and make it reachable from a LiveKit Cloud agent — good question. You cannot give the agent direct shell access to your machine, so you need to make the service reachable over the internet. Here are practical options: • Use a tunneling tool (quickest): ngrok, Cloudflare Tunnel, or localhost.run to expose your local port to a public HTTPS URL the agent can call. Example:
ngrok http 3000
. • Host the service publicly: deploy the service to a cloud VM/container (Render, Fly.io, etc.) and call its public domain or IP from the agent. • Run the agent where the service is reachable: deploy the agent to an environment that can access your service (self-hosted agent or same VPC/network). Notes on implementation: • From the agent, call the public URL with standard HTTP libraries or define it as an HTTP tool in Agent Builder. • Store any credentials or API keys using LiveKit secrets features so they aren’t hard-coded. Which option fits your setup best (tunnel, public hosting, or self-hosted agent)? I can give step-by-step commands for the one you pick. Sources: https://blog.livekit.io/announcing-agent-builder-a-pathway-to-code/#/portal/signup| | Deploying to custom environments | LiveKit Docs | Zscaler proxy configuration | Log collection | LiveKit Docs
s
wouldn't this ass a delay on each call making the agent less responsive ?
t
You're right to consider latency. Short answer: yes — tunneling can add some extra latency, but how noticeable it is depends on the tunnel provider, your network, and the call pattern. Here’s what to expect and ways to reduce impact. What adds latency • Tunnel hop: traffic goes through the tunnel provider’s relay before reaching your machine, adding RTT. • Your local network: upload bandwidth and Wi‑Fi/mobile links can be the biggest contributor. • Tunnel provider quality: some providers introduce minimal overhead, others more. How noticeable it will be • Single HTTP request/response for non-realtime tasks: usually a small extra 20–200ms depending on provider and location — often acceptable. • High-frequency or low-latency interactions (real-time control, voice/video signalling): added latency and jitter can degrade responsiveness. Ways to reduce latency • Use a low-latency tunnel provider or choose an exit region near your agent’s region (Cloudflare Tunnel or a geographically close ngrok endpoint). • Ensure a solid local network (wired connection, good upload bandwidth). • Batch or debounce calls so the agent makes fewer HTTP requests. • Cache responses locally in the agent when possible. • Move latency-sensitive logic into the agent itself or host the service publicly in a cloud region near LiveKit Cloud or the agent. • Consider self-hosting the agent in the same network/VPC as the service to eliminate public hops. If you tell me the nature of the calls (frequency, latency goal in ms, realtime vs. background) and where your agent runs regionally, I can recommend the best option and concrete providers/configuration. Would you like that?
s
would it be possible to launch it within the same cloud with docker ?
t
You cannot launch arbitrary Docker containers inside LiveKit Cloud. LiveKit Cloud is fully managed and doesn't provide shell/container access. Options to run a service "in the same cloud" or eliminate public hops: • Embed the HTTP service inside your agent: run the HTTP server as part of the agent process (so agent and service share the same runtime). This keeps calls local to the agent and has minimal latency. • Deploy the service to a cloud container/VM in the same region as your agent (Render, Fly.io, AWS/GCP/Azure). Host the service publicly and point the agent to that internal/cloud URL to reduce network latency. • Self-host the agent and service on the same host or VPC: run both as Docker containers (or compose/k8s) so they communicate over the LAN/VPC without public tunneling. Tell me which fits your constraints (embed in agent, public cloud deploy, or self-host), the kind of runtime you use for agents (language/runtime), and your latency target; I’ll give concrete steps or sample commands.
s
how i can Embed the HTTP service inside my agent
t
You're asking how to embed an HTTP service inside your LiveKit Cloud agent so the agent can call it locally (low latency). That is possible — run a lightweight HTTP server inside the same agent process/container and have the agent call it via localhost. Below are concise, runnable examples and key notes so you can pick the language you use. Python (FastAPI + Uvicorn) 1. Add deps: fastapi, uvicorn, httpx. 2. Run the HTTP server in a background task/thread and use
httpx
from your agent logic:
Copy code
# agent.py
from fastapi import FastAPI
import uvicorn
import threading
import asyncio
import httpx
from livekit import cli, JobContext, WorkerOptions  # adjust imports to your SDK

app = FastAPI()

@app.post("/process")
async def process(payload: dict):
    return {"processed": payload.get("input", "") + " -> ok"}

def run_server():
    uvicorn.run(app, host="0.0.0.0", port=8000)

async def entrypoint(ctx: JobContext):
    thread = threading.Thread(target=run_server, daemon=True)
    thread.start()
    await asyncio.sleep(0.5)  # allow server to start

    async with httpx.AsyncClient() as client:
        r = await client.post("http://localhost:8000/process", json={"input":"hello"})
        await ctx.room.local_participant.publish_data(r.json())

if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
Node.js (Express + fetch) 1. Add dep: express. 2. Start Express before/inside the agent entrypoint and call `http://localhost:8000`:
Copy code
// agent.ts
import express from "express";
import { defineAgent, JobContext } from "@livekit/agents"; // adjust as needed
const app = express();
app.use(express.json());

app.post("/process", (req, res) => {
  res.json({ processed: `${req.body.input} -> ok` });
});

app.listen(8000, () => console.log("HTTP service on :8000"));

export default defineAgent({
  entry: async (ctx: JobContext) => {
    const res = await fetch("http://localhost:8000/process", {
      method: "POST",
      headers: {"Content-Type":"application/json"},
      body: JSON.stringify({ input: "hello" }),
    });
    const json = await res.json();
    await ctx.room.localParticipant.publishData(JSON.stringify(json));
  },
});
Dockerfile note • EXPOSE the port (e.g., 8000) in your Dockerfile. Internal
localhost
calls do not require external routing. Key benefits and constraints • Benefits: near-zero latency (calls over
localhost
), no public tunnel, tight coupling for latency-sensitive logic. • Constraints: the HTTP service is internal to the agent container — not reachable from outside LiveKit Cloud unless you host it separately or self-host the agent. Next step Which runtime are you using for your agent (Python or Node), and do you want a minimal example repo or Dockerfile snippets for deployment? I can provide those next. Sources: Voice AI quickstart | LiveKit docs | Deploying to production | LiveKit Docs | Agent server | LiveKit docs
s
where the external service is run in this code ?
t
Good question — in those examples the "external" service actually runs inside the same agent container/process, not on a separate machine. • Python example: the FastAPI server is started in a background thread inside the agent process (via
threading.Thread(target=run_server, daemon=True)
), so the service binds to
0.0.0.0:8000
inside the container and the agent calls it at
http://localhost:8000
. • Node example: Express is started in the same process before the agent's entrypoint runs (
app.listen(8000)
), so the agent calls
http://localhost:8000
. Because both server and agent share the same container, calls go over localhost (near-zero latency) and the service is not externally exposed unless you explicitly publish the port or deploy the service separately.
s
but run_server how is it implemented ?
t
You're right to ask — here's the concrete implementation of
run_server
and how it integrates with the agent for the Python example. Python (FastAPI + Uvicorn) — full focused snippet:
Copy code
from fastapi import FastAPI
import uvicorn
import threading
import asyncio
import httpx
from livekit import cli, JobContext, WorkerOptions  # adjust to your SDK

app = FastAPI()

@app.post("/process")
async def process(payload: dict):
    return {"processed": payload.get("input", "") + " -> ok"}

def run_server():
    # This blocks while running the ASGI server; run it in a daemon thread
    uvicorn.run(app, host="0.0.0.0", port=8000, log_level="info")

async def entrypoint(ctx: JobContext):
    # Start the HTTP server in a background thread so it doesn't block the agent
    thread = threading.Thread(target=run_server, daemon=True)
    thread.start()

    # Optionally wait a short time for the server to be ready
    await asyncio.sleep(0.5)

    # Agent calls the local service over localhost
    async with httpx.AsyncClient() as client:
        r = await client.post("http://localhost:8000/process", json={"input": "hello"})
        await ctx.room.local_participant.publish_data(r.json())

if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
Key points: •
run_server
calls
uvicorn.run(...)
, which starts the ASGI loop and blocks — that's why we start it in a daemon thread. • The agent's logic runs in the async `entrypoint`; it can call
http://localhost:8000
with minimal latency. • Keep the thread daemonized so it won't prevent graceful shutdown of the agent process. • If you prefer fully async, you can run Uvicorn programmatically with
uvicorn.Server
and
asyncio.create_task
instead of threads. Want the programmatic async version example next, or a Node.js equivalent?