onetool
Sandboxed Lua runtime for LLM tool use.
The Problem
LLM agents typically need dozens of specialized tools (calculator, date formatter, string manipulator, JSON parser, base64 encoder, hash generator, etc.). This creates two problems:
- Token costs add up: Each tool call requires a round-trip to the LLM provider. Complex tasks need multiple calls, and you pay for every token exchanged.
- Tool proliferation: Each new tool requires API design, documentation, and testing. Tools don't compose well. And you're always limited by what you thought to create.
What if the LLM could solve problems programmatically instead of making multiple tool calls? We already have the perfect interface for that: programming languages.
The Solution
onetool provides a sandboxed Lua REPL that LLMs can use as a tool.
LLMs are already trained on programming languages. By giving them code execution instead of specialized tools, you reduce token costs (one tool call instead of many) while increasing flexibility. State persists between calls for multi-step reasoning. It's safe by design with comprehensive sandboxing.
Prior art: Cloudflare and Anthropic have explored similar approaches with their Code Mode and MCP code execution respectively.
Framework Support
onetool provides adapters for popular Rust LLM frameworks:
- genai - Multi-provider LLM client (OpenAI, Google, Anthropic)
- mistral.rs - Fast local model inference
- rig - Modular LLM application framework
- aisdk - Rust port of Vercel's AI SDK
See Framework Integration for usage details.
Quick Start: LLM Integration
Core REPL Usage
use Repl;
// Create the sandboxed Lua runtime
let repl = new?;
// Execute Lua code
let response = repl.eval?;
// Access results
println!; // "4"
println!; // (print() output)
The REPL maintains state between calls, so variables and functions persist:
repl.eval?;
repl.eval?;
let result = repl.eval?; // "30"
LLM Framework Integration
onetool provides ready-to-use adapters for popular LLM frameworks:
- genai -
LuaRepl::new(&repl)withdefinition()andcall()methods - mistralrs -
LuaRepl::new(repl)withdefinition()andcall()methods - rig -
LuaRepl::new(repl)implementsTooltrait - aisdk -
LuaRepl::new(repl)with.tool()method
Each adapter handles tool definition registration and execution for its framework. See Framework Integration for detailed usage.
Real Example: What Can It Do?
Here's an actual interaction from the included example:
User: "What's the sum of the 10 first prime numbers?"
LLM calls lua_repl with:
{
"source_code": "
local primes = {}
local num = 2
while #primes < 10 do
local is_prime = true
for i = 2, math.sqrt(num) do
if num % i == 0 then
is_prime = false
break
end
end
if is_prime then
table.insert(primes, num)
end
num = num + 1
end
local sum = 0
for _, p in ipairs(primes) do
sum = sum + p
end
return sum
"
}
Response: {
"result": "129",
"output": ""
}
LLM: "The sum of the first 10 prime numbers is 129."
The LLM wrote a complete algorithm, executed it safely, and got the answer - all without needing a specialized "prime number calculator" tool.
Framework Integration
genai Adapter
Feature flag: genai
The genai adapter provides seamless integration with the genai multi-provider LLM client.
Key Methods:
LuaRepl::new(&repl)- Creates the adapter.definition()- Returnsgenai::chat::Toolfor registration.call(&tool_call)- Executes tool call and returnsToolResponse
Example:
use ;
let repl = new?;
let lua_repl = new;
// Register with genai client
let chat_req = new
.with_tools;
// Execute tool calls
let tool_response = lua_repl.call;
Full example: examples/genai-basic.rs
mistralrs Adapter
Feature flag: mistralrs
The mistralrs adapter integrates with mistral.rs for fast local model inference.
Key Methods:
LuaRepl::new(repl)- Creates the adapter.definition()- Returnsmistralrs::Toolfor registration.call(&tool_call)- Executes tool call and returns result string
Example:
use ;
let repl = new?;
let lua_repl = new;
// Register with mistralrs model
let messages = new
.add_message
.set_tools;
// Execute tool calls
let result = lua_repl.call;
Full example: examples/mistralrs-basic.rs
rig Adapter
Feature flag: rig
The rig adapter implements the Tool trait from rig-core.
Key Methods:
LuaRepl::new(repl)- Creates the tool (implementsTooltrait)
Example:
use ;
let repl = new?;
let lua_tool = new;
// Use with rig agents
let agent = client
.agent
.tool
.build;
Full example: examples/rig-basic.rs
aisdk Adapter
Feature flag: aisdk
The aisdk adapter provides integration with aisdk.
Key Methods:
LuaRepl::new(repl)- Creates the adapter.tool()- Returns a tool function for use with aisdk
Example:
use ;
let repl = new?;
let lua_repl = new;
// Use with aisdk
let result = builder
.model
.prompt
.with_tool
.build
.generate_text
.await?;
Full example: examples/aisdk-basic.rs
Tool Definition System
onetool includes a complete tool definition system that works with any LLM framework:
use tool_definition;
// Tool metadata
NAME // "lua_repl"
DESCRIPTION // Full description for LLM context
PARAM_SOURCE_CODE // "source_code"
// JSON Schema (framework-agnostic)
let schema = json_schema;
Framework-specific helpers:
// genai (requires "genai" feature)
let tool = genai_tool;
// For mistralrs, rig, aisdk: use the adapter's .definition() method
// See Framework Integration section above
Compatible with:
- OpenAI function calling
- Google Gemini function calling
- Anthropic tool use
- Any JSON Schema-based tool system
Security Model
Safe by Design
- Sandboxed Lua 5.4 runtime - Dangerous operations blocked at the language level
What's Available
- String manipulation (
string.*) - Table operations (
table.*) - Math functions (
math.*) - UTF-8 support (
utf8.*) - Safe OS functions (
os.time,os.date) - All Lua control flow and data structures
What's Blocked
- File I/O (
io,file) - Network access
- Code loading (
require,dofile,load*) - OS commands (
os.execute,os.getenv, etc.) - Metatable manipulation
- Coroutines
- Garbage collection control
Key Features
For LLM Integration:
- Code execution as a single tool (reduces need for specialized tools)
- Built-in tool definitions (OpenAI, Google, Anthropic compatible)
- JSON Schema generation
- Comprehensive documentation in tool description
For Developers:
- Drop-in integration with genai, mistralrs, rig, and aisdk libraries
- Separate
print()output from return values - Clear error messages
- Type-safe Rust API via mlua
For LLM Agents:
- Persistent state between calls (variables, functions, tables)
- Runtime introspection via
docsglobal - Can solve multi-step problems programmatically
- Self-documenting environment
Installation
Basic REPL only (no LLM framework):
[]
= "0.0.1-alpha.5"
With genai:
[]
= { = "0.0.1-alpha.5", = ["genai"] }
= "0.5"
With mistralrs:
[]
= { = "0.0.1-alpha.5", = ["mistralrs"] }
= { = "https://github.com/EricLBuehler/mistral.rs.git" }
With rig:
[]
= { = "0.0.1-alpha.5", = ["rig"] }
= "0.3"
With aisdk:
[]
= { = "0.0.1-alpha.5", = ["aisdk"] }
= "0.2"
Feature flags:
| Feature | Includes | Description |
|---|---|---|
genai |
json_schema |
genai adapter + tool definition |
mistralrs |
json_schema |
mistralrs adapter + tool definition |
rig |
json_schema |
rig-core Tool implementation |
aisdk |
json_schema |
aisdk #[tool] macro integration |
json_schema |
- | JSON Schema generation (included by all above) |
Note: Currently in alpha - API may change.
Running the Examples
All examples solve the same problem (sum of first 10 primes = 129) to demonstrate consistent behavior across frameworks.
LLM Framework Examples
genai (multi-provider client):
# or GEMINI_API_KEY, etc.
Source: examples/genai-basic.rs
mistralrs (local inference):
Downloads and runs Phi-3.5-mini locally. No API key required.
Source: examples/mistralrs-basic.rs
rig (modular framework):
Source: examples/rig-basic.rs
aisdk (Vercel AI SDK port):
Source: examples/aisdk-basic.rs
Interactive REPL
Test the sandboxed environment directly:
This lets you experiment with Lua code and understand what the LLM sees. No API key required.
custom-functions (runtime extension):
Shows how to extend the runtime with custom Rust functions. Includes interactive REPL for testing.
Source: examples/custom-functions.rs
Extending the Runtime
onetool allows you to extend the Lua runtime with custom Rust functions, enabling domain-specific capabilities for your LLM agents.
Extension Methods
There are two approaches to adding custom functions:
Method 1: Post-Initialization (with_runtime())
Best for adding functions after creating the REPL:
use Repl;
let repl = new?;
// Add a custom function
repl.with_runtime?;
// Now callable from Lua
let result = repl.eval?;
Use when:
- Adding functions to an existing REPL
- Functions don't need to interact with sandboxing
- Simpler initialization flow
Method 2: Pre-Sandboxing (new_with())
Best for complex initialization scenarios:
use ;
let lua = new;
// Set up custom globals
lua.globals.set?;
// Register custom functions
let fetch = lua.create_function?;
lua.globals.set?;
// Apply sandboxing AFTER custom setup
apply?;
let repl = new_with?;
Use when:
- Need to set up complex state before sandboxing
- Custom functions require special initialization
- Building framework adapters
Complete Example
See examples/custom-functions.rs for a complete demonstration including:
- Multiple function patterns (simple, error handling, stateful)
- Stateful closures with Arc + Atomic
- Error propagation from Rust to Lua
- Documentation registration
- Interactive testing
Registering Documentation
Make your custom functions discoverable via the docs system:
use ;
repl.with_runtime?;
The LLM can then query docs["my_function"] at runtime to understand available functions.
API Overview
Full API documentation available at docs.rs/onetool.
Why Lua?
These were the criteria for choosing the execution language:
- Interpreted: We can't depend on a compile-eval loop
- Easy to embed: The runtime needs to live inside the host application
- Easy to sandbox: Giving too much power to an LLM can be dangerous
- Simple and expressive: LLMs need to write small, correct snippets
- Strong standard library: Especially for string manipulation
- Mature and well-known: Editor plugins, documentation, familiarity
Lua checks all these boxes. It's widespread enough (neovim config language, game scripting) that LLMs are well-trained on it.
Use Cases
- LLM agents that need computation capabilities
- AI assistants with multi-step reasoning
- Applications requiring safe user-generated code execution
Project Status
This is still a toy project. Use with care - everything may break, and I might decide to change everything tomorrow.
- Version: 0.0.1-alpha.5
- API Stability: Expect breaking changes
- Production Ready: No
The core concept is stable (sandboxed Lua REPL for LLMs), but the implementation and API surface are experimental.
Development
Building:
Nix Support:
Running Examples:
# Framework examples (requires API keys for genai, rig, aisdk)
# Interactive REPL
Architecture
For implementation details, see:
src/runtime/mod.rs- Lua runtime definitionsrc/runtime/docs.rs- Runtime documentation implementationsrc/runtime/sandbox.rs- Sandboxing implementationsrc/tool_definition.rs- Tool integration system
Key patterns:
- Nil-based sandboxing (simple, effective)
- Output capture via mpsc channels
- Persistent Lua state across invocations
- Runtime documentation system
License & Contributing
License: MIT - Copyright 2026 Caio Augusto Araujo Oliveira
Contributing:
- Early stage project - feedback welcome!
- Issues and PRs appreciated
Built with mlua.