Skip to main content

Building on Freyja

Freyja keeps its core small and publishes the contracts around it. Storage, memory, models, embedders, tools, token counting and hooks are each a trait, and a crate of your own can implement any of them without forking Freyja or asking for a change upstream. A Redis backend, a Faiss search, a llama.cpp model or an OpenTelemetry exporter is an ordinary crate that depends on freyja, and an agent takes it exactly as it takes the built-in pieces.

There are no private extension points. Freyja's own pieces implement the same public traits: Client is a Model and an Embedder, InMemoryStorage and JsonlStorage are Storage, Window is a Memory, HeuristicCounter is a TokenCounter, and Tracing is a Hook. Anything they can do, your crate can do.

What you can build​

To buildImplementFor example
A place to keep conversationsStorageRedis, DynamoDB, MongoDB, SQLite, a schema you already have
A search over your own dataToolFaiss, Qdrant, pgvector, Elasticsearch, a company wiki
A model Freyja cannot reach over HTTPModelllama.cpp, candle, mistral.rs, a provider with its own wire format
A wrapper around any modelModelA response cache, fallback between providers, a router, a rate limiter
Local or custom embeddingsEmbedderfastembed, candle, ONNX Runtime, an in-house embedding service
A rule for what the model seesMemoryRetrieval over older turns, summaries with your own prompt
Splitting documentsNothing. Index::add takes any iterator of stringstext-splitter, a splitter of your own. See Splitting documents
Exact token countsTokenCountertiktoken, Hugging Face tokenizers
Observability and controlHookOpenTelemetry, Langfuse, Prometheus, per-user quotas, audit logs
Tools from somewhere elseToolThe tools of an OpenAPI description or a plugin registry. MCP servers are already covered by freyja-mcp

A vector database plugs in as a VectorStore, and an Index over it does the embedding and offers the search tool. A search that needs more than the nearest texts, such as metadata filters, is a tool of your own.

One crate can implement several traits. A crate for a local runtime might ship a Model, an Embedder and a TokenCounter that all use the same loaded weights.

Each page​

Every trait has a page with the same parts: when to implement it, the trait itself, what each method must and must not do, one or two complete examples, and how to test an implementation with no network and no key. Every complete example on these pages is compiled and tested against Freyja 0.7. The few that use a crate Freyja does not depend on, such as a tokenizer, say so.

Publishing a crate​

freyja-sqlx keeps conversations in Postgres, MySQL or SQLite and is built the way a community crate is: a separate crate, depending on freyja, with a feature for each database.

Name and dependency​

Name the crate freyja-<what it adds>, such as freyja-redis or freyja-faiss, so people can find it next to freyja-sqlx.

Depend on freyja alone for the traits, with no features you do not need. The tracing feature only adds the Tracing hook, and a crate that does not use it should leave it off so it does not turn it on for its users.

[dependencies]
freyja = "0.7"

Freyja is before 1.0. Cargo treats "0.7" as any 0.7.x release and not 0.8, because a 0.x minor release may break. Publish a new release of your crate for each Freyja minor release you support, and say in your README which one it is.

Take traits, not types​

Accept any implementation where your crate takes one from its user. A search tool that takes Arc<dyn Embedder> works with a Client, a local embedder or a test double. One that takes Client works with a Client only. The same holds for impl Model rather than Client, and impl TokenCounter rather than HeuristicCounter.

Return your own types, not boxed traits. A user can always erase RedisStorage into Box<dyn Storage>, but cannot get a RedisStorage back out of a box.

Test against the contract​

freyja::testing ships with Freyja and needs no network and no key:

HelperChecks
storage_conformanceEvery rule a Storage must keep. Run it from your backend's tests.
ScriptedModelAn agent that uses your tool, memory or hook, against answers you script
RecorderSaves a real run so it can be replayed as a ScriptedModel

A backend that storage_conformance passes works under every agent, memory and conversation Freyja has. Each trait's page shows the tests worth writing for that trait.

Say which rules you keep​

Some rules are choices. A storage backend may keep the state slot or not, a model may stream or replay, and a memory may support compacting or not. Write in your README which ones your crate implements, so a user knows, for example, whether a summary survives a restart.

The stability promise​

Every trait returns boxed futures, so each stays usable as a trait object: an agent holds Arc<dyn Tool>, Arc<dyn Memory> and Arc<dyn Model>, and a backend can be erased behind Box<dyn Storage>.

A method added to a trait later arrives with a default, so an implementation written today keeps compiling. Enums and structs that may grow, such as Error, StopReason and GenerateResponse, are #[non_exhaustive]: build them with their constructors, and end a match on one with a _ arm.

See also​

  • Concepts for how the pieces fit together
  • Architecture for how Freyja is built inside
  • Adding a dialect for teaching Freyja itself a new wire format, which is a change to Freyja rather than a crate