Embedding Graphrt
The graphrt binary is a thin layer over the libraries. Embedding them gives you full control over services, persistence and transport.
The graphrt binary is a thin layer over the libraries. Embedding them gives
you full control over services, persistence and transport.
[dependencies]graphrt-core = "0.6"graphrt-llm = "0.6" # optional: llm / agent / classifier / extractor nodesgraphrt-http = "0.6" # optional: http-request nodegraphrt-code = "0.6" # optional: code nodegraphrt-mcp = "0.6" # optional: MCP toolsfutures = "0.3"tokio = { version = "1", features = ["rt", "macros"] }Running a workflow
Section titled “Running a workflow”use std::sync::Arc;use futures::StreamExt;use graphrt_core::{Engine, Event, Graph, Registry, RunInput, Services};
async fn run(yaml: &str) -> Result<(), Box<dyn std::error::Error>> { // 1. Node kinds: builtins plus whatever crates you enable. let mut registry = Registry::with_builtins(); graphrt_llm::register(&mut registry); graphrt_http::register(&mut registry);
// 2. Compile once; share the Arc<Graph> across any number of runs. let graph = Arc::new(Graph::from_text(yaml, ®istry)?);
// 3. Host services that nodes look up at run time. let services = Services::new() .with(graphrt_llm::ModelRegistry::from_env()?) .with(graphrt_http::HttpClient::new(Default::default())?);
let engine = Engine::new(graph).with_services(services);
// 4. Polling the stream drives the run; nothing is spawned. let mut run = engine.run(RunInput::new().input("question", "hi").sys("user_id", "u-1")); let handle = run.handle(); // abort / pause / update_variable from elsewhere while let Some(event) = run.next().await { if let Event::RunSucceeded { outputs, .. } = &event { println!("{outputs:?}"); } } let _ = handle; Ok(())}Engine::run_to_end(input) is a convenience that drains the stream and returns
the terminal event.
Clocks and executors
Section titled “Clocks and executors”The engine never depends on a specific async runtime. Timers (retry delays,
timeouts, run duration) come from the Clock trait. With the default tokio
feature TokioClock is used; on other executors implement Clock and pass it
with Engine::with_clock. NoTimerClock runs everything without timers
(useful in tests and in wasm hosts without a timer).
Limits
Section titled “Limits”use graphrt_core::engine::Limits;let engine = engine.with_limits(Limits { max_steps: 500, max_duration_ms: Some(60_000), default_node_timeout_ms: Some(30_000), ..Limits::default()});Ordered answer streaming
Section titled “Ordered answer streaming”Engine events carry raw token chunks (Event::NodeChunk) as soon as they are
produced, even from branches that are not yet known to reach an answer. Use
ResponseAssembler to turn events into the user-facing answer text in
template order:
use graphrt_core::response::ResponseAssembler;
let mut assembler = ResponseAssembler::new(engine.graph());while let Some(event) = run.next().await { for chunk in assembler.push(&event) { print!("{}", chunk.text); }}Pause and resume
Section titled “Pause and resume”use graphrt_core::{Event, Snapshot};
if let Event::RunPaused { reasons, snapshot } = event { store.save(&snapshot.run_id, &snapshot.to_json()?)?; // your storage}
// Later, possibly in another process (same workflow document):let snapshot = Snapshot::from_json(&store.load(run_id)?)?;let mut inputs = indexmap::IndexMap::new();inputs.insert("review".to_owned(), graphrt_core::Value::from_json(serde_json::json!({ "action": "approve", "values": {"note": "ok"}})));let run = engine.run(RunInput::new().resume(snapshot, inputs));Snapshots never contain env.* values; supply them again in RunInput::env.
The engine rejects a snapshot whose graph fingerprint does not match.
Custom nodes
Section titled “Custom nodes”use std::sync::Arc;use futures::future::BoxFuture;use graphrt_core::node::{parse_config, NodeResult};use graphrt_core::{Node, NodeContext, NodeError, NodeOutput, Registry, Selector, Value};use serde::Deserialize;
#[derive(Debug, Deserialize)]struct UppercaseConfig { input: Selector,}
#[derive(Debug)]struct Uppercase(UppercaseConfig);
impl Node for Uppercase { fn run(&self, ctx: NodeContext) -> BoxFuture<'_, NodeResult> { Box::pin(async move { let text = ctx .get(&self.0.input) .and_then(|v| v.as_str().map(str::to_uppercase)) .ok_or_else(|| NodeError::invalid_input("input must be a string"))?; Ok(NodeOutput::new().with("text", text).into()) }) }}
let mut registry = Registry::with_builtins();registry.register("uppercase", |doc| Ok(Arc::new(Uppercase(parse_config(doc)?))));Rules for nodes:
- Read inputs from
ctx.pool(an immutable snapshot). Return outputs and writes inNodeOutput; never mutate shared state. NodeOutput::outputsis agraphrt_core::Variablesmap. The engine stores it as the node’s pool namespace and emits it inNodeSucceededwithout copying, so build it once, with.with("name", value)oroutputs.insert. String-literal names cost no allocation.- Use
NodeError::fatalfor errors a retry cannot fix andNodeError::newfor transient ones; the engine only retries retryable errors. - Stream partial output with
ctx.chunk(field, text); report structured progress withctx.progress(name, data). A node that streams many chunks can keep the field name in anArc<str>and callctx.chunk_shared(&field, text)to avoid allocating it per chunk (Event::NodeChunk::fieldis anArc<str>). - Return
NodeOutcome::Paused(PauseRequest { .. })to wait for the host; you receive your savedstateand the host input inctx.resume. - Override
role()for branch (NodeRole::Branch, setNodeOutput::branch), response or container nodes.
Custom services
Section titled “Custom services”Services is a type map. Register anything Send + Sync + 'static and fetch it
in a node with ctx.service::<T>() or ctx.require_service::<T>("name"):
#[derive(Debug)]struct Crm { base_url: String }
let services = Services::new().with(Crm { base_url: "https://crm.internal".into() });// in a node: let crm = ctx.require_service::<Crm>("CRM client")?;Custom model providers
Section titled “Custom model providers”Implement graphrt_llm::ChatModel and register it under a provider name used in
workflow model.provider fields:
use std::sync::Arc;use futures::future::BoxFuture;use graphrt_llm::{ChatModel, ChatRequest, ChatStream, LlmError, ModelRegistry};
#[derive(Debug)]struct MyModel;
impl ChatModel for MyModel { fn chat(&self, request: ChatRequest) -> BoxFuture<'_, Result<ChatStream, LlmError>> { Box::pin(async move { // Call your backend and stream `ChatDelta`s (Text, Reasoning, ToolCall, Usage, Finish). let _ = request; Err(LlmError::Config("not implemented".into())) }) }}
let models = ModelRegistry::new().with("my-provider", Arc::new(MyModel));Any OpenAI-compatible endpoint works without code: see
graphrt_llm::openai::OpenAiConfig or the GRAPHRT_OPENAI_COMPAT_<NAME>_*
environment variables.
Native tools
Section titled “Native tools”Expose Rust functions to tool and agent nodes:
use std::sync::Arc;use graphrt_core::tool::{ToolOutput, ToolRegistry, ToolSpec};use graphrt_core::tool_node::NativeTools;use serde_json::json;
let tools = NativeTools::new().with( ToolSpec { name: "add".into(), description: "Add two numbers".into(), parameters: json!({"type": "object", "properties": {"a": {"type": "number"}, "b": {"type": "number"}}, "required": ["a", "b"]}), }, |args| async move { let sum = args["a"].as_f64().unwrap_or(0.0) + args["b"].as_f64().unwrap_or(0.0); Ok(ToolOutput::text(sum.to_string())) },);
let mut registry = ToolRegistry::new();registry.add("math", Arc::new(tools));let services = graphrt_core::Services::new().with(registry);// workflow: tools: [math/add] or type: tool, provider_id: math, tool_name: addMCP servers are added the same way: graphrt_mcp::connect_all(&configs).await?
returns a ToolRegistry with each server registered as <name> and mcp:<name>.