π¦ Building AI Agents in Rust – part 11
Last Updated on July 23, 2026 by Editorial Team
Author(s): Enzo Lombardi
Originally published on Towards AI.
Crossing into C
Part 10 got Eugene talking to DwarfStar without a socket, but it didnβt get rid of the process boundary. DwarfStarProvider still spawns ds4 as a child, writes a line to its stdin, and reads lines back until a ds4> prompt reappears in stdout. That works, and itβs honest about its own limit: itβs parsing terminal output that was never meant to be a wire format, and it trusts a banner string to mean βthis turn is done.β The REPL keeps its KV cache warm across turns, which was the whole point, but Eugene still canβt ask it for a sampling temperature, a tokenβs logprob, or a clean signal that generation actually finished versus the pipe just going quiet for a moment.

This post shows how to replace the subprocess/REPL text protocol with direct in-process FFI by using DwarfStarβs public C header boundary (ds4.h). It explains that DwarfStar exposes an engine and per-session KV-cache timeline, including prefix-aware session sync that reuses KV state by diffing the provided token prefix against the live checkpoint. With that seam, the author builds a Rust Provider that calls ds4_engine/session functions directly (no pipes), compiles the required C/ObjectiveβC sources into a Rust binary via build.rs (targeting Metal/CUDA as DwarfStar recommends), and wraps unsafe FFI narrowly to ensure resources are freed exactly once and C-side errors are surfaced as Rust Result failures. The resulting complete() implementation ends generation using the engineβs EOS token (not prompt strings), gaining token-level sampling control and explicit cache-reuse semantics while paying costs in C build integration and carefully pinned/unstable ABI assumptions. The article closes by emphasizing that the agent loop remains unchanged across integration strategiesβHTTP, subprocess, or FFIβbecause the Provider trait isolates transport and execution details.
Read the full blog for free on Medium.
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.
Published via Towards AI
Towards AI Academy
We Build Enterprise-Grade AI. We'll Teach You to Master It Too.
15 engineers. 100,000+ students. Towards AI Academy teaches what actually survives production.
Start free β no commitment:
β 6-Day Agentic AI Engineering Email Guide β one practical lesson per day
β Agents Architecture Cheatsheet β 3 years of architecture decisions in 6 pages
Our courses:
β AI Engineering Certification β 90+ lessons from project selection to deployed product. The most comprehensive practical LLM course out there.
β Agent Engineering Course β Hands on with production agent architectures, memory, routing, and eval frameworks β built from real enterprise engagements.
β AI for Work β Understand, evaluate, and apply AI for complex work tasks.
Note: Article content contains the views of the contributing authors and not Towards AI.