Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions livekit-agents/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ speechmatics = ["livekit-plugins-speechmatics>=1.8.0"]
spatius = ["livekit-plugins-spatius>=1.8.0"]
spitch = ["livekit-plugins-spitch>=1.8.0"]
tavus = ["livekit-plugins-tavus>=1.8.0"]
thegrid = ["livekit-plugins-thegrid>=1.8.0"]
trugen = ["livekit-plugins-trugen>=1.8.0"]
turn-detector = ["livekit-plugins-turn-detector>=1.8.0"]
ultravox = ["livekit-plugins-ultravox>=1.8.0"]
Expand Down
15 changes: 15 additions & 0 deletions livekit-plugins/livekit-plugins-thegrid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# The Grid plugin for LiveKit Agents

Support for LLM with [The Grid](https://thegrid.ai/), an OpenAI-compatible inference marketplace.

See [https://docs.livekit.io/agents/integrations/llm/](https://docs.livekit.io/agents/integrations/llm/) for more information.

## Installation

```bash
pip install livekit-plugins-thegrid
```

## Pre-requisites

For credentials, you'll need a The Grid account and API key. Credentials can be passed directly or via `THEGRID_API_KEY` environment variable.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2026 LiveKit, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""The Grid plugin for LiveKit Agents

Support for LLM with The Grid, an OpenAI-compatible inference marketplace.
"""

from livekit.agents import Plugin

from .llm import LLM
from .log import logger
from .version import __version__

__all__ = ["LLM", "__version__"]


class TheGridPlugin(Plugin):
def __init__(self) -> None:
super().__init__(__name__, __version__, __package__, logger)


Plugin.register_plugin(TheGridPlugin())

# Cleanup docs of unexported modules
_module = dir()
NOT_IN_ALL = [m for m in _module if m not in __all__]

__pdoc__ = {}

for n in NOT_IN_ALL:
__pdoc__[n] = False
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Copyright 2026 LiveKit, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import os

import httpx
import openai

from livekit.agents.llm import ToolChoice
from livekit.agents.types import (
NOT_GIVEN,
NotGivenOr,
)
from livekit.agents.utils import is_given
from livekit.plugins.openai import LLM as OpenAILLM

from .models import TheGridChatModels

THEGRID_BASE_URL = "https://api.thegrid.ai/v1"


class LLM(OpenAILLM):
def __init__(
self,
*,
model: str | TheGridChatModels = "text-standard",
api_key: NotGivenOr[str] = NOT_GIVEN,
base_url: NotGivenOr[str] = THEGRID_BASE_URL,
client: openai.AsyncClient | None = None,
user: NotGivenOr[str] = NOT_GIVEN,
temperature: NotGivenOr[float] = NOT_GIVEN,
parallel_tool_calls: NotGivenOr[bool] = NOT_GIVEN,
tool_choice: NotGivenOr[ToolChoice] = NOT_GIVEN,
top_p: NotGivenOr[float] = NOT_GIVEN,
timeout: httpx.Timeout | None = None,
):
"""
Create a new instance of The Grid LLM.

``api_key`` must be set to your The Grid API key, either using the argument or by
setting the ``THEGRID_API_KEY`` environmental variable.
"""
resolved_key = api_key if is_given(api_key) else os.environ.get("THEGRID_API_KEY", "")
# A caller-supplied client owns its own authentication, so only require a
# key when this class has to construct the client itself.
if client is None and not resolved_key:
raise ValueError(
"THEGRID_API_KEY is required, either as argument or set "
"THEGRID_API_KEY environmental variable"
)

super().__init__(
model=model,
api_key=resolved_key or NOT_GIVEN,
base_url=base_url,
client=client,
user=user,
temperature=temperature,
parallel_tool_calls=parallel_tool_calls,
tool_choice=tool_choice,
top_p=top_p,
timeout=timeout,
)

@property
def model(self) -> str:
return self._opts.model

@property
def provider(self) -> str:
return "The Grid"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import logging

logger = logging.getLogger("livekit.plugins.thegrid")
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import Literal

# The Grid addresses capability tiers rather than a specific lab's model name;
# a tier routes to a current model for that tier. The `*-latest` ids pin a
# particular lab instead. `GET https://api.thegrid.ai/v1/models` is the
# authoritative list.
TheGridChatModels = Literal[
"text-standard",
"text-prime",
"text-max",
"code-standard",
"code-prime",
"code-max",
"agent-standard",
"agent-prime",
"agent-max",
"bytedance-pro-latest",
"claude-opus-latest",
"deepseek-pro-latest",
"gemini-pro-latest",
"glm-latest",
"gpt-sol-latest",
"kimi-latest",
"minimax-latest",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2026 LiveKit, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__version__ = "1.8.0"
44 changes: 44 additions & 0 deletions livekit-plugins/livekit-plugins-thegrid/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "livekit-plugins-thegrid"
dynamic = ["version"]
description = "The Grid LLM plugin for LiveKit Agents"
readme = "README.md"
license = "Apache-2.0"
requires-python = ">=3.10.0"
authors = [{ name = "LiveKit", email = "hello@livekit.io" }]
keywords = ["voice", "ai", "realtime", "audio", "video", "livekit", "thegrid", "the-grid"]
classifiers = [
"Intended Audience :: Developers",
"License :: OSI Approved :: Apache Software License",
"Topic :: Multimedia :: Sound/Audio",
"Topic :: Multimedia :: Video",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3 :: Only",
]
dependencies = [
"livekit-agents[openai]>=1.8.0",
]

[project.urls]
Documentation = "https://docs.livekit.io"
Website = "https://livekit.io/"
Source = "https://github.com/livekit/agents"

[tool.hatch.version]
path = "livekit/plugins/thegrid/version.py"

[tool.hatch.build.targets.wheel]
packages = ["livekit"]

[tool.hatch.build.targets.sdist]
include = ["/livekit"]

[tool.uv]
exclude-newer = "7 days"
exclude-newer-package = { livekit = "0 days", livekit-agents = "0 days" }
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ livekit-plugins-spatius = { workspace = true }
livekit-plugins-spitch = { workspace = true }
livekit-plugins-tavus = { workspace = true }
livekit-plugins-telnyx = { workspace = true }
livekit-plugins-thegrid = { workspace = true }
livekit-plugins-trugen = { workspace = true }
livekit-plugins-turn-detector = { workspace = true }
livekit-plugins-ultravox = { workspace = true }
Expand Down
71 changes: 71 additions & 0 deletions tests/test_plugin_thegrid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from __future__ import annotations

import pytest

from livekit.agents import Agent, AgentSession, RunContext, function_tool, llm
from livekit.plugins.thegrid import LLM

pytestmark = pytest.mark.plugin("thegrid")

# text-standard is the general conversation tier; agent-standard is tuned for
# multi-step tool use. Both accept the OpenAI tool-calling fields.
CHAT_MODEL = "text-standard"
TOOL_MODEL = "agent-standard"


class WeatherAgent(Agent):
def __init__(self) -> None:
super().__init__(instructions="You are a helpful assistant.")

@function_tool
async def get_weather(self, ctx: RunContext, location: str) -> str:
"""Get the current weather for a location.
Args:
location: The city name
"""
return f"The weather in {location} is sunny, 72°F."


@pytest.mark.asyncio
async def test_chat():
"""Basic chat completion returns a non-empty assistant message."""
async with LLM(model=CHAT_MODEL) as model, AgentSession(llm=model) as sess:
await sess.start(Agent(instructions="You are a helpful assistant."))
result = await sess.run(user_input="Say hello in exactly one word.")
result.expect.next_event().is_message(role="assistant")
result.expect.no_more_events()


@pytest.mark.asyncio
async def test_function_call():
"""LLM can invoke a tool and the result is returned."""
async with LLM(model=TOOL_MODEL) as model, AgentSession(llm=model) as sess:
await sess.start(WeatherAgent())
result = await sess.run(user_input="What is the weather in Tokyo?")
result.expect.next_event().is_function_call(
name="get_weather", arguments={"location": "Tokyo"}
)
result.expect.next_event().is_function_call_output(
output="The weather in Tokyo is sunny, 72°F."
)
result.expect.next_event().is_message(role="assistant")
result.expect.no_more_events()


@pytest.mark.asyncio
async def test_streaming():
"""Streaming chat returns content via the LLM directly."""
async with LLM(model=CHAT_MODEL) as model:
chat_ctx = llm.ChatContext()
chat_ctx.add_message(role="system", content="You are a helpful assistant.")
chat_ctx.add_message(role="user", content="Count from 1 to 5.")

stream = model.chat(chat_ctx=chat_ctx)
text = ""
async for chunk in stream:
if chunk.delta and chunk.delta.content:
text += chunk.delta.content
await stream.aclose()

assert len(text) > 0, "Expected non-empty streaming response"
assert "3" in text, "Expected the count to include '3'"
17 changes: 16 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.