When IBM Watson looked incredible
Back in 2019, I was working on embedded automation on a Proof of concept with IBM Watson. At the time, you basically had an interface where you defined intents, which were triggered against different user prompts, and these triggers caused a webhook to be called on your edge device.
At the time, this already looked sophisticated, but what if I told you this is now doable on bare metal on your ESP32?
Embedding model on bare metal
Last week at the office, we discussed whether we could run AI models directly on the ESP32 boards we use in our home automation systems.
We had two use cases in mind: building a local smart home controller (e.g., Amazon Echo, Google Nest, etc.) and helping operators configure devices on the network through natural language.
During that discussion, a colleague came across an article by Clelia Bertelli on how to run an embedding model on bare metal.
The article immediately caught our attention, so I spent the following week experimenting with the idea.
The article's promise was really interesting: a model called Potion-base-2M is 7.5MB, runs on an ESP32-S3 with a no_std tokenizer, and lets you query preloaded knowledge.
This translates, in short terms, to the fact that we can use the model to build a searchable knowledge base from a collection of predefined texts, such as:
const CORPUS: [&str; 6] = [
"Quarterly revenue grew 12% year over year.",
"Qdrant is a vector search engine written in Rust.",
"The ESP32-S3 is a dual-core microcontroller with Wi-Fi and Bluetooth.",
"Paris is the capital of France and sits on the river Seine.",
"Water boils at 100 degrees Celsius at sea level.",
"Rust guarantees memory safety without a garbage collector.",
];
let corpus_vectors: Vec<Vec<f32>> = CORPUS.iter().map(|text| embedder.embed(text)).collect();
and then query it:
let query = "What is the capital city of France?";
let vector = embedder.embed(query);
let mut ranked: Vec<(usize, f32)> = corpus_vectors
.iter()
.map(|candidate| cosine_similarity(&vector, candidate))
.enumerate()
.collect();
ranked.sort_unstable_by(|a, b| b.1.total_cmp(&a.1));
let (index, _score) = ranked[0];
println!("{top_answer}", top_answer = CORPUS[index]);
And we'll have Paris is the capital of France and sits on the river Seine. printed to the serial.
Now, this may not seem too interesting if we stop here. We have no generative AI, no particular actions, no context or conversation functionalities.
So, where do we go from here?
From a research project to a Rust crate
After testing Clelia's PoC, I was thrilled, and I immediately started to imagine that this project could evolve fast. I was like, "Can we generate text with this?", "Can we have a conversation?" "Can we have context?" "Can I use this to control devices at home, like an improved version of Alexa?"
The sky was the limit, if the sky is 16MB of flash.
I forked the project, and as a Rust developer, I immediately decided to model the code as a crate anyone could use.
That’s how esp-agent was born: a no_std Rust library for building local, embedding-powered assistants on Espressif microcontrollers, combining semantic search with structured commands and conversational state.
In deciding what to focus for implementing it as a crate, I opted for three different fronts:
- Providing a stable, easy-to-use API across compatible Espressif chips with sufficient flash capacity and memory-mapping support.
- Exploring which embedding models could realistically run within the hardware constraints.
- Understanding which features we could build on top of these models.
What models we can actually run
With just 16MB of flash, I knew I was quite limited on the models we could run on the board.
The neat thing about Potion is that it’s a static embedding model. In potion-base-2M, each of the 29,528 tokens has a row of 64 numbers. To embed a sentence, the ESP32 breaks it into tokens, looks up their rows in flash, and averages them. By default, it also normalizes the result, so we end up with a 64-number vector we can compare with the vectors we already have. The table stays in flash the whole time, which is how we get away with having a model several megabytes larger than the chip’s internal RAM. Potion 2M model card.
That also explains what the crate can load. So, what can we put in there today?
- potion-base-2M takes 7.64 MiB including the vocabulary. It’s the general English model I use in the examples, where the whole application image comes in at about 8.2 MB and targets boards with 16 MB of flash.
- potion-base-4M takes 14.85 MiB and scores higher on Minish Lab’s retrieval benchmarks. This is where the 16 MB sky starts to feel a bit low. The whole image has to fit in the application partition, and the chip has to map the model into its address space.
- pubmedbert-base-embeddings-2M takes 7.90 MiB. It’s a similar flash budget to Potion 2M, but NeuML built it for biomedical English. So if your ESP32 is searching PubMed abstracts instead of a device manual, there’s a model for that too.
For home automation, I’d start with Potion 2M. But there’s a catch in that averaging step. Take “turn on the kitchen light and turn off the hall light” and swap kitchen and hall. You’ve asked for the opposite thing in both rooms, and the model is still averaging the same token vectors. So, how do we get from a vector that helps us find the right sentence to an assistant that knows which light to turn off? That’s where the rest of the crate comes in.
Context and turns
If we wanted to use this to control devices at home, we had to make it work across more than one prompt.
Finding a similar command was a good start, but what happens when I say “the lamp” and there are two lamps in the room?
I’d expect the assistant to ask me which one, and I’d expect to answer without repeating the whole thing.
> Dim the lamp to 37%
Which lamp, the desk lamp or the floor lamp?
> The desk one
The desk lamp is at 37 percent.
> Turn it off
The desk lamp is off.
Each line I type is a turn. In the first turn, the agent has the action and the brightness, but "the lamp" could mean either of the two devices. So it keeps the command and the choices around while it waits for my answer. When I say "the desk one", it only has to look at the two lamps it just offered.
Once the command succeeds, it remembers which lamp it acted on. That’s what "it" refers to in the next turn.
This is where the embedding model and the firmware have different jobs. The model can find a similar example for what I typed. The firmware still has to read my actual request to get the action and the brightness. If I say "dim the lamp" and leave out the percentage, the firmware can ask "To what brightness?" and use my next answer to finish the command.
That got me a little closer to what I had in mind when I started the fork. I could ask the ESP32 to do something; it could ask me a question back, and I could answer it like I normally would. All of that happens on the board.
But implementing turn-based workflows can get messy
The conversation with the two lamps looks simple enough. But imagine we have to write the firmware for it. We need to know that "the lamp" could mean either lamp, that "the desk one*" answers a question we just asked, and that "the living room lights" means we should act on both. And that’s before we get to all the ways someone can ask us to turn a light on.
I could write all those rules by hand, but adding another device would mean going back through the code that matches names, handles replies, and figures out which devices belong together. This is Rust, so of course I ended up writing a macro. Well, two of them.
The first one, targets!, lets me write down what’s in the room and what people might call it.
static LAMPS: TargetCatalog = targets! {
device Desk = 1, "desk lamp" {
replies ["desk", "desk one"];
}
device Floor = 2, "floor lamp" {
replies ["floor", "floor one", "the one by the sofa"];
}
choose "lamp" from [Desk, Floor];
group ["living room", "living room lights"] = [Desk, Floor];
};
The difference between choose and group is something I wanted to see in the declaration. Both point to the same two lamps, but "turn on the lamp" should get me a question, while "turn on the living room lights" should switch on both. And when the agent asks which lamp I mean, "the one by the sofa" is enough to pick the floor lamp from the choices it just offered.
The second macro, TurnCommands, handles what I can ask those lamps to do. I already had a Rust enum for the actions, so I put the phrases right next to the variants.
#[derive(TurnCommands)]
#[commands(previous = "it")]
enum Action {
#[command("turn on {target}", "turn {target} on")]
TurnOn,
#[command("turn off {target}", "turn {target} off")]
TurnOff,
#[command("dim {target} to {level} percent", "dim {target} to {level}%")]
SetBrightness { level: Percent },
}
Now "turn on the desk lamp*" and "turn the desk lamp on" both give the firmware a TurnOn action. For dimming, the parser pulls out the target and converts the brightness to a Percent. So 37% and 37 percent both work, and a value like 150% fails when it tries to parse the brightness. The previous = "it" line is what lets a command refer back to the target the agent remembers.
The two macros meet when I hand the action enum and the lamp catalog to the agent’s turn handler. From there, my firmware gets an action and the IDs of the devices it should act on. I still write the part that talks to the lamps, and I can still handle a follow-up like "To what brightness?" in the firmware. But if someone at home starts calling the floor lamp "the sofa lamp", I know exactly where to add that name.
But target catalogs are never static
After implementing the turn-based workflow, I realized it had a big problem: most domotics systems either discover devices on the network or receive catalogs from a coordinator. The targets! didn't let me define devices on the network on the go.
Because of that, I've decided to add a TargetLookup trait and the DynamicTargetCatalog to allow adding and removing devices while running the firmware:
use esp_agent::{DynamicTargetCatalog, target};
let mut catalog = DynamicTargetCatalog::new();
let mut lamp_ids = Vec::new();
let mut room_ids = Vec::new();
for device in coordinator.devices() {
catalog.insert(target!(device.id, device.name, replies = device.aliases))?;
room_ids.push(device.id);
if device.is_lamp {
lamp_ids.push(device.id);
}
}
catalog.insert_choice("lamp", lamp_ids)?;
catalog.insert_group(["living room"], room_ids)?;
let mut handler = Action::handler(&catalog, dispatch);
let outcome = agent.turn("turn on desk lamp", &mut handler);
Translations on the go
Another problem is that this kind of device may have support for different languages, either embedded or received from a coordinator.
Unfortunately, though, the #[commands(previous = "it")] bound me, making it hard to support different languages.
The idea was to add a vocabulary that we can set for the handler to have either a command for targets and a way to refer to the referred context entity (it):
static ITALIAN: Vocabulary<Action> = vocabulary! {
Action::TurnOn => explicit ["accendi {target}"], previous ["accendila", "accendilo"];
Action::TurnOff => explicit ["spegni {target}"], previous ["spegnila", "spegnilo"];
};
handler.set_vocabulary(ITALIAN);
and eventually also a runtime counterpart for translations coming from an external resource:
let mut german = RuntimeVocabulary::<Action>::new();
german.insert_explicit("TurnOff", "schalte {target} aus")?;
german.insert_previous("TurnOff", "ausschalten")?;
handler.set_vocabulary(german);
Can the ESP32 write its own answer?
So, back to one of the first questions I had when I forked the project. Can we generate text with this thing?
With Potion, we can already ask “Why is the status LED blinking twice?” and find a paragraph in the manual called "Two flashes during setup". That’s the job of an embedding model. As we saw earlier, it turns both pieces of text into numbers we can compare. I get to ask the question in my own words, and the answer I get back is the one somebody wrote in the manual.
A generative model works differently. Give it the question, and it starts writing an answer, one small piece of text at a time. Each time it adds a piece, it looks at what it has written so far to work out what comes next. And yes, someone actually got this running on an ESP32-S3. The esp32-ai project runs a model that writes short stories at about 10 tokens per second, all on the board. The model alone takes 14.9 MB. Suddenly my 16 MB sky looks pretty low again.
Could we use both? Sure. Potion could find the paragraph about the LED, and a generator could take that paragraph and the question and explain it in a couple of sentences. That’s what people mean by retrieval-augmented generation, or RAG. With the current Potion firmware coming in at about 8.2 MB, that particular story model and Potion won’t fit together in 16 MB of flash. A smaller generator might.
But for esp-agent, I keep coming back to the desk lamp. If I ask it to dim to 37%, I want the firmware to talk to the right lamp and tell me whether it worked. If it failed, a perfectly worded “The desk lamp is at 37 percent” is about the least useful answer it could give me. The same goes for the blinking LED. If two flashes mean the device is waiting to be paired, I can put that sentence in the manual once, test that the agent finds it, and get it back over serial even when the network is down.
That’s why I’d stick with the embedding model for esp-agent.
Conclusion
By combining a small embedding model with structured commands, typed parameters, and explicit conversational state, we can build assistants that are flexible enough to interact with humans while keeping action execution under the control of the firmware.
For home automation in particular, running everything locally also means that we don't have to send our requests to an external service, and our devices can remain functional even when the Internet connection goes down.
Of course, there's still a lot of work to do. Language support, more flexible command parsing, and better tooling for dynamic device catalogs are all things I'd like to explore further.
With version 0.2.0 of esp-agent released today, my next step is to move beyond the examples and start building a proof of concept for a local, AI-powered smart home controller. I'm particularly interested in how far we can push this approach with real devices, rather than just printing answers to a serial console.
I'll document the project and share the results on this blog, including what doesn't work.
In the meantime, if you'd like to experiment with local AI on your own ESP32, you can check out the esp-agent GitHub repository.



