Getting started
From nothing to a working call, then to an agent.
Install
cargo add freyja
cargo add tokio --features macros,rt-multi-thread
Or in Cargo.toml directly:
[dependencies]
freyja = "0.7"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
Requires Rust 1.88 or later, verified in CI on every commit.
You supply the async runtime. Freyja exposes async fn and never spawns, so it pulls in no runtime of its own and uses reqwest, serde, serde_json, schemars, futures-util, and its companion macro crate. The optional tracing feature adds tracing integration.
Set a key
Freyja reads credentials from the environment.
| Provider | Variable |
|---|---|
EndpointPreset::OpenAi | OPENAI_API_KEY |
EndpointPreset::Gemini | GEMINI_API_KEY |
EndpointPreset::Anthropic | ANTHROPIC_API_KEY |
# .env
OPENAI_API_KEY=sk-...
Those three are the whole built-in list, on purpose. Every other endpoint, DeepSeek, Groq, OpenRouter, a local Ollama, or your own gateway, is reached with Client::custom and is no less supported for it.
Add dotenvy as a dev-dependency if you want .env loaded automatically. Nothing in Freyja requires it.
Your first call
use freyja::{Agent, Client, EndpointPreset};
#[tokio::main]
async fn main() {
let provider = EndpointPreset::OpenAi;
let client = match Client::from_env(provider) {
Ok(client) => client,
Err(error) => {
eprintln!("{error}");
return;
}
};
match Agent::new(client).ask("Name three Rust crates.").await {
Ok(run) => println!("{}", run.answer),
Err(error) => eprintln!("request failed: {error}"),
}
}
Agent::new takes the client, ask sends one question and returns a Run. run.answer is the text, and run.usage is the tokens the run spent. With no tools registered, the run is one request and one answer. To send a bare request with no agent around it, see Calling the model directly.
from_env returns an Error::MissingApiKey that names the variable rather than panicking when the variable is missing, so a misconfigured deployment tells you what is wrong instead of sending an unauthenticated request.
Note what the agent does not set: no model, no temperature, no token cap. Every unset field means "the provider decides", which is what makes this same call valid on all three providers. See Concepts.
Switch provider
Change one line:
let provider = EndpointPreset::Anthropic;
Nothing else moves. Freyja translates the same neutral request the agent builds into a completely different wire format.
Portable does not mean identical, though. Each provider refuses a different slice of the request, so one using previous_response_id fails on Anthropic or Chat Completions. You get an error before the network call rather than a wrong answer. The provider pages say which is which.
Reach a provider that is not built in
use freyja::{Client, Dialect};
let client = Client::custom(
Dialect::OpenAiChat,
"DeepSeek",
"https://api.deepseek.com/v1",
std::env::var("DEEPSEEK_API_KEY").unwrap(),
);
Four things: which wire format, a name for error messages, the root URL, and the key. That one call covers most of the hosted inference market, because most vendors copy a format Freyja already speaks. See Custom providers.
Add a tool
This is the reason to use this library rather than a thinner one. Annotate a typed function and Freyja derives its JSON Schema and executor:
use freyja::{Agent, tool};
#[tool(description = "adds two numbers together", strict = true)]
fn add(a: i64, b: i64) -> i64 {
a + b
}
let agent = Agent::new(client).tool(add).max_turns(5);
let run = agent.ask("What is 20 + 22?").await?;
println!("{}", run.answer);
The model does not run anything. It asks, and Agent finds the requested tool, runs it, sends the result back and repeats until the model answers or max_turns stops it. For several turns, agent.conversation(InMemoryStorage::new()) and send keep the history. The loop in full is Building an agent, and writing it by hand is Calling the model directly.
Next
| The design in five ideas | Concepts |
| The agent loop, step by step | Building an agent |
| Agents, conversations and memory | Agents, Conversations, Memory |
| Hooks, approval, teams and testing | Hooks, Human approval, Multi-agent, Testing |
| What is not implemented yet | Features |