Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Quick Start

From zero to a working agent in 5 minutes.

Prerequisites

  • Rust 1.85+
  • An API key for OpenAI or any OpenAI-compatible endpoint

1. Rust: an agent with a tool

export OPENAI_API_KEY=sk-...
export OPENAI_MODEL=gpt-4o-mini   # optional
cargo run -p langchainrust --example quickstart_agent

The example (crates/lc/examples/quickstart_agent.rs) builds a FunctionCallingAgent with Calculator, runs one tool-calling loop, and prints the answer:

use std::sync::Arc;

use langchainrust::agents::{AgentExecutor, FunctionCallingAgent};
use langchainrust::core::tools::BaseTool;
use langchainrust::tools::Calculator;
use langchainrust::{OpenAIChat, OpenAIConfig};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let llm = OpenAIChat::new(OpenAIConfig::from_env_result()?);
    let tools: Vec<Arc<dyn BaseTool>> = vec![Arc::new(Calculator::new())];

    let agent = FunctionCallingAgent::new(
        llm,
        tools.clone(),
        Some("You are a concise assistant. Use tools when needed.".to_string()),
    );

    let executor = AgentExecutor::new(Arc::new(agent), tools).with_max_iterations(5);
    println!("{}", executor.invoke("What is 25 * 37?".to_string()).await?);
    Ok(())
}

2. Python: call the runtime over HTTP

Terminal 1 — start the OpenAI-compatible server:

cargo run -p langchainrust --example openai_server --features openai-server
# OpenAI-compatible server on http://127.0.0.1:8080

Terminal 2 — install the thin client and call it:

pip install -e sdks/python

python - <<'PY'
from langchainrust import Client

c = Client("http://127.0.0.1:8080", model="gpt-4o-mini")
print(c.chat("What is Rust?").content)
PY

Any OpenAI SDK works against the same endpoint:

from openai import OpenAI

client = OpenAI(base_url="http://127.0.0.1:8080/v1", api_key="unused")
print(client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "hi"}],
))

3. TypeScript: the same endpoint

npm install langchainrust
# or, from this repository: npm install ./sdks/typescript
import { Client } from "langchainrust";

const c = new Client("http://127.0.0.1:8080", "gpt-4o-mini");
console.log((await c.chat("What is Rust?")).content);

Troubleshooting

SymptomFix
OPENAI_API_KEY errorExport the key; the config fails fast when it is missing
Port already in useLC_ADDR=127.0.0.1:9090 before starting the server
unknown feature openai-serverAdd --features openai-server
Python ModuleNotFoundErrorRun pip install -e sdks/python from the repo root

Next

  • Agents — hooks, budgets, approvals, subagents
  • MCP — expose and consume tools
  • A2A — agent-to-agent
  • Evaluation — verifiers and regression gates