Run agents with local models
Section titled “Run agents with local models”Run Antigravity agents on your machine using local models without an API key or internet connection.
Depending on your setup, you can configure local model execution using the following classes:
LiteRTAgentConfig: Runs.litertlmmodel checkpoints (such as Gemma 4 26B A4B) directly on-device using Google AI Edge’s LiteRT runtime.LocalOpenAIAgentConfig: Connects to an external local server with an OpenAI-compatible API, such as Ollama, LM Studio, or vLLM.
On-device execution with LiteRT
Section titled “On-device execution with LiteRT”LiteRTAgentConfig runs local models using the LiteRT runtime. When the agent
starts, the SDK spins up and manages a local loopback server backed by your
.litertlm checkpoint. Supported hardware backends (gpu for Apple Silicon
Metal and NVIDIA CUDA, or npu) are detected automatically at startup.
Set up LiteRT and Gemma 4 26B A4B
Section titled “Set up LiteRT and Gemma 4 26B A4B”Follow these steps to install the dependencies and import the model checkpoint:
Note: A machine with at least 24 GB of VRAM or unified memory is recommended to run the Gemma 4 26B A4B checkpoint (which downloads about 16.8 GB).
-
Create and activate a virtual environment:
python3 -m venv .venv source .venv/bin/activate -
Install the SDK and the
litert-lmpackage:pip install google-antigravity litert-lm -
Use the
litert-lmCLI to import the Gemma 4 26B A4B checkpoint:litert-lm import \ --from-huggingface-repo=litert-community/gemma-4-26B-A4B-it-litert-lm \ gemma-4-26B-A4B-it-gpu.litertlm \ gemma4-26bThis command downloads about 16.8 GB and registers the checkpoint at
~/.litert-lm/models/gemma4-26b/model.litertlm.Note: On macOS, if
litert-lm importfails with an SSL certificate verification error, installcertifiand exportSSL_CERT_FILEbefore retrying:pip install certifi export SSL_CERT_FILE=$(python3 -c "import certifi; print(certifi.where())")
Run a local LiteRT agent
Section titled “Run a local LiteRT agent”Once you have configured LiteRT, run a local LiteRT agent by passing the
.litertlm model file in LiteRTAgentConfig. For example:
Note: Use the full path to your model file or use
os.path.expanduser()because tilde (~) isn’t expanded automatically.
import asyncio
import os
from google.antigravity import Agent, LiteRTAgentConfig
MODEL_PATH = os.path.expanduser(
"~/.litert-lm/models/gemma4-26b/model.litertlm"
)
async def main():
config = LiteRTAgentConfig(model_path=MODEL_PATH)
async with Agent(config) as agent:
response = await agent.chat("What files are in the current directory?")
async for token in response:
print(token, end="", flush=True)
print()
if __name__ == "__main__":
asyncio.run(main())
If you want to let the local agent edit files and run shell commands in a
project directory, set workspaces and attach a tool policy. For example:
import asyncio
import os
from google.antigravity import Agent, LiteRTAgentConfig
from google.antigravity.hooks import policy
async def main():
workspace = os.path.expanduser("~/my-local-project")
os.makedirs(workspace, exist_ok=True)
config = LiteRTAgentConfig(
model_path=os.path.expanduser(
"~/.litert-lm/models/gemma4-26b/model.litertlm"
),
workspaces=[workspace],
policies=[policy.allow_all()],
)
async with Agent(config) as agent:
response = await agent.chat(
"Create a static webpage index.html with a dark-mode toggle."
)
async for token in response:
print(token, end="", flush=True)
print()
if __name__ == "__main__":
asyncio.run(main())
Vision support
Section titled “Vision support”Use LiteRTAgentConfig to analyze images on-device with vision-capable
.litertlm checkpoints (such as Gemma 4 26B A4B). This supports offline and
hybrid visual workflows such as UI verification, screenshot debugging, and
diagram analysis.
At startup, LiteRTAgentConfig inspects your checkpoint with
litert_lm.ModelInfo to detect vision support and select the default hardware
backend ("gpu" or "cpu"). You can also pass an explicit vision_backend
override. Pass images alongside text prompts in agent.chat() using
from_file() or Image (see
Multimodal attachments):
import asyncio
import os
from google.antigravity import Agent, LiteRTAgentConfig, from_file
MODEL_PATH = os.path.expanduser(
"~/.litert-lm/models/gemma4-26b/model.litertlm"
)
async def main():
config = LiteRTAgentConfig(
model_path=MODEL_PATH,
# Optional: override the auto-detected vision backend ("gpu" or "cpu")
# vision_backend="gpu",
)
async with Agent(config) as agent:
response = await agent.chat([
"Inspect this UI screenshot and check for layout issues:",
from_file("screenshot.png"),
])
async for token in response:
print(token, end="", flush=True)
print()
if __name__ == "__main__":
asyncio.run(main())
Prefix caching
Section titled “Prefix caching”LiteRTAgentConfig automatically caches conversation prefixes across
sequential agent turns with zero configuration changes.
When the conversation prefix and tool schemas match the previous turn, the
managed LiteRT-LM session reuses its in-memory KV cache instead of recomputing
static system instructions, tool schemas, and prior history. LiteRT-LM prefills
only newly added prompts and tool outputs, which reduces latency during
multi-step tool calling.
LiteRT-LM persists compiled model graphs next to model_path by default. To
override the on-disk compilation cache directory, create the directory first
and pass its expanded path via cache_dir. LiteRT-LM does not expand tilde
(~) automatically, and litert_lm.Engine requires the directory to already
exist:
cache_dir = os.path.expanduser("~/.litert-lm/cache")
os.makedirs(cache_dir, exist_ok=True)
config = LiteRTAgentConfig(
model_path=MODEL_PATH,
cache_dir=cache_dir,
)
LiteRT configuration options
Section titled “LiteRT configuration options”LiteRTAgentConfig automatically applies the .lightweight() preset when
initialized. This preset restricts built-in tools to core coding operations,
trims system instructions for smaller context windows, and sets a default
context compaction threshold sized for the 64k LiteRT KV-cache.
In addition to standard AgentConfig
fields (tools, policies, hooks, mcp_servers, subagents, workspaces,
and system_instructions), LiteRTAgentConfig
supports the following options:
| Key | Type | Default | Allowed values | Description |
|---|---|---|---|---|
model_path | str | Required | Absolute .litertlm file path | Path to the local .litertlm model checkpoint file. |
backend | Lite or str | Lite | 'gpu', 'npu', 'cpu' | Hardware backend used for model inference. |
vision_backend | Lite, str, or None | None (auto-detected) | 'gpu', 'npu', 'cpu', None | Hardware backend override for vision processing. When None, Lite inspects litert_ and selects 'gpu' or 'cpu'. |
audio_backend | Lite, str, or None | None | 'gpu', 'npu', 'cpu', None | Hardware backend override for audio processing. |
enable_ | bool | False | True, False | Enables multi-token prediction for faster generation. |
cache_dir | str or None | None | Existing expanded directory path or None | Overrides the default on-disk compilation cache location (when None, LiteRT-LM persists compiled artifacts next to model_path). Tilde (~) is not expanded automatically, and the directory must already exist on disk (use os. and os.). |
port | int | 0 | 0 or TCP port | Local loopback port for the managed LiteRT server (0 selects an available port automatically). |
compaction_ | Compaction or None | token_ | Compaction or None | Overrides the default context compaction threshold derived from the 64k LiteRT KV-cache. |
External OpenAI-compatible servers
Section titled “External OpenAI-compatible servers”To connect to an external local server that exposes an OpenAI-compatible API
(such as Ollama, LM Studio, or vLLM), use LocalOpenAIAgentConfig. Call
.lightweight() on the configuration to apply the tool and prompt
optimizations designed for local models if needed.
Note: Don’t use
LocalOpenAIAgentConfigto connect tolitert-lm serve. UseLiteRTAgentConfiginstead so the SDK manages the LiteRT server lifecycle for you.
For example, you can connect an agent to a local Ollama server running Gemma 4:
import asyncio
from google.antigravity import Agent, LocalOpenAIAgentConfig
async def main():
config = LocalOpenAIAgentConfig(
model="gemma4:26b",
base_url="http://localhost:11434/v1",
).lightweight()
async with Agent(config) as agent:
response = await agent.chat("What files are in this directory?")
async for token in response:
print(token, end="", flush=True)
print()
if __name__ == "__main__":
asyncio.run(main())
LocalOpenAIAgentConfig
supports all standard AgentConfig
options along with these connection parameters:
model(string orModelTarget, optional): Model identifier registered on your local server (such as"gemma4:26b"or"gemma-4-26B-A4B-it").base_url(string, optional): URL of the local server’s OpenAI-compatible endpoint (such as"http://localhost:11434/v1"for Ollama or"http://localhost:1234/v1"for LM Studio).
Sample code
Section titled “Sample code”To explore full working code examples, check out the following resources:
local_models.py: Getting-started script forLiteRTAgentConfigandLocalOpenAIAgentConfig.multimodal.py: Attaching images, documents, and audio withImage.from_file(),Document.from_file(), andAudio.from_file().- Antigravity Hybrid Gauntlet: Hybrid workflow that pairs a cloud Gemini planner with local Gemma 4 26B agents for on-device code auditing and patching.