cargo-ai
🌐 Overview
cargo-ai is a lightweight, Rust-based framework for building no-code AI agents using clean, declarative, JSON configs. Agents compile into fast, secure binaries—perfect for local machines, servers, and embedded Linux devices, with broader embedded support planned.
Lightweight AI agents. Built in Rust. Declared in JSON.
✨ Features
- Declarative, No-Code Agents – Define agent logic in JSON
- Rust-Powered – Safe, fast, and portable across environments
- Compile-Time Safety – Minimal runtime overhead; standalone binaries
- Fully Local & Secure – All logic executes client-side (no phoning home)
- Embedded-Ready – Agents compile into binaries suitable for servers and embedded Linux devices, with broader embedded support planned
- Composable CLI – Scaffold, build, and run agents via
cargo aicommands
📦 Installation
Base Install
-
Install Rust & Cargo
Follow the official guide:
Install Rust & Cargo -
Install cargo-ai
Once Cargo is available, installcargo-aifrom source:Verify installation:
Create a Sample Agent
-
Hatch a Sample Agent (AdderAgent – a sample “Hello World” style agent that adds 2 + 2):
Generic form:
Example:
Run the Sample Agent
-
Run the compiled agent with OpenAI GPT:
Generic form:
Example (AdderAgent with GPT-4o):
⚙️ CLI Usage
Cargo AI Commands
The base cargo ai command provides subcommands for managing agents:
)
Hatch Command
The hatch command creates a new AI agent from a JSON config:
<name> Name
)
Agent Commands
Once hatched, your agent is compiled as a standalone binary.
Example with AdderAgent (binary name: AdderAgent):
🌦️🤖 Create Your Own Weather Agent with JSON
We’ll walk through a WeatherAgent.json example step-by-step—prompt, expected response schema, optional resource URLs, and actions.
To define a custom agent, you’ll use a JSON file that specifies:
- The prompt to send to the AI/transformer server
- The expected response schema (properties returned)
- (Optional) Resource URLs provided to the server alongside the prompt
- A set of actions to run, depending on the agent’s response
The steps below show how to create the WeatherAgent, but once defined, running it is as simple as:
# 1. Hatch your WeatherAgent from a JSON config
)
# 2. Run your WeatherAgent with a server, model, and token
# Expected output if raining tomorrow:
# bring an umbrella
1) Define the Prompt
The prompt is the natural language instruction or question you send to the AI/transformer server.
It frames what the agent is supposed to do. You can phrase it as a question, a request, or a directive.
Example from WeatherAgent.json:
"prompt": "Will it rain tomorrow between 9am and 5pm? (Consider true if over 40% for any given hour period.)"
You can edit the text to suit your agent’s purpose—for example, summarizing an article, checking stock prices, or answering domain-specific questions.
2) Define the Response Schema
The agent_schema describes the shape of the response you expect from the AI/transformer server.
Behind the scenes, this schema is also used to generate the corresponding Rust structures.
You can define fields as:
boolean→ true/false valuesstring→ text valuesnumber→ floating-point numbers (f64)integer→ whole numbers (i64)
Example from WeatherAgent.json:
"agent_schema":
3) Define Resource URLs
The resource_urls section lists optional external data sources your agent can use.
Each entry includes:
url: the API endpoint or resource locationdescription: a short explanation of what the resource provides
These URLs are passed to the AI/transformer server alongside the prompt, giving the agent more context to work with.
Example from WeatherAgent.json:
"resource_urls":
Note: The weather forecast URL in the example is configured for Cincinnati (latitude/longitude values). Update these values and the description to match your own location.
4) Define Actions
The actions section specifies what the agent should do based on the response.
It follows the JSON Logic format for conditions.
Currently, actions can run a command-line executable (exec).
Future versions will support additional action types.
Example from WeatherAgent.json:
"actions":
In this example:
- If
rainingis true, the agent prints “bring an umbrella.” - If
rainingis false, the agent prints “bring sunglasses.”