A ReAct (Reasoning and Acting) agent implementation in Elixir that thinks using Lua code. Inspired by smolagents.
Luagents implements a ReAct loop where:
- The agent receives a task from the user
- It uses an LLM (Large Language Model) to generate Lua code to think through the problem step-by-step
- The Lua code can call tools and reason about results
- The agent continues until it finds a final answer
Luagents supports multiple LLM providers through a pluggable architecture:
- Anthropic Claude (via Anthropix) - Cloud-based, high-quality responses
- Ollama - Local models, privacy-focused, customizable
Important Note: This agent's performance is directly tied to the quality of the underlying language model. Using a model with strong reasoning and coding capabilities is essential for reliable results.
Add luagents to your dependencies in mix.exs:
def deps do
[
{:luagents, "~> 0.1.0"}
]
endSet your Anthropic API key using one of these methods:
- Environment variable (recommended):
export ANTHROPIC_API_KEY=your-api-key- Pass directly when creating an agent:
agent = Luagents.Agent.new(
llm: Luagents.LLM.new(provider: :anthropic, api_key: "your-api-key")
)- Install and run Ollama
- Pull a model (e.g.,
ollama pull mistral) - Create an agent with Ollama provider:
agent = Luagents.Agent.new(
llm: Luagents.LLM.new(provider: :ollama, model: "mistral")
)# Create an agent (uses Anthropic Claude by default)
agent = Luagents.create_agent(name: "MyBot")
# Run a task
{:ok, result} = Luagents.run_with_agent(agent, "What is 2 + 2?")# Use Ollama with a specific model
agent = Luagents.create_agent(
name: "OllamaBot",
llm: Luagents.create_llm(:ollama, model: "mistral")
)
# Use Anthropic Claude with a specific model
agent = Luagents.create_agent(
name: "ClaudeBot",
llm: Luagents.create_llm(:anthropic, model: "claude-3-opus-20240229")
)# Define custom tools using deflua or anonymous functions
tools = %{
power: Luagents.Tool.new(
"power",
"Calculate power (base^exponent)",
[
%{name: "base", type: :number, description: "Base number", required: true},
%{name: "exp", type: :number, description: "Exponent", required: true}
],
fn [base, exp] -> :math.pow(base, exp) end
)
}
# Create agent with custom tools and configuration
agent = Luagents.create_agent(
name: "MathBot",
llm: Luagents.create_llm(:ollama, model: "mistral"),
max_iterations: 15,
tools: tools
)
{:ok, result} = Luagents.Agent.run(agent, "What is 10 to the power of 2?")Luagents provides several ready-to-use tools that can be easily added to your agents:
Parse, encode, and format JSON data within your Lua agent code. Enables seamless conversion between JSON strings and Lua tables for working with APIs and structured data.
parse(json_string)- Parse JSON strings into Lua tablesencode(data)- Encode Lua tables/values into JSON stringspretty(data)- Pretty-print JSON with formatting
Make HTTP requests to external APIs and services directly from your agent's Lua code. Supports all common HTTP methods with customizable headers and request bodies.
get(url, headers?)- Make HTTP GET requestspost(url, body, headers?)- Make HTTP POST requestsput(url, body, headers?)- Make HTTP PUT requestsdelete(url, headers?)- Make HTTP DELETE requests
Log messages from your agent's reasoning process to Elixir's Logger at various levels. Useful for debugging, monitoring agent behavior, and capturing important events during task execution.
debug(message, metadata?)- Log debug-level messagesinfo(message, metadata?)- Log informational messageswarning(message, metadata?)- Log warningserror(message, metadata?)- Log errorslog(level, message, metadata?)- Log at a specific level with optional metadata
The agent thinks in Lua code like this:
thought("I need to perform a calculation")
local a = 10
local b = 20
local sum = add(a, b)
observation("The sum is " .. sum)
local result = multiply(sum, 2)
thought("Multiplying by 2 gives " .. result)
final_answer("The result is " .. result)Before opening a pull request, please open an issue first.
git clone https://github.com/doomspork/luagents.git
cd luagents
mix deps.get
mix test
Once you've made your additions and mix test passes, go ahead and open a PR!
Luagents is Copyright © 2025 doomspork. It is free software, and may be redistributed under the terms specified in the LICENSE file.