# agdb in the AI era

> Blog: agdb for AI agents and LLM apps — graph memory, queries without injection, one file per agent

Applications built around large language models need to remember things: what the user said, what the agent did, which facts it learned and where they came from. Most of that is not a list of documents but a web of relations: people, projects, decisions and the conversations connecting them. That is a graph, and it is where `agdb` fits naturally.

## Memory is a graph

An agent's long-term memory has a few kinds of things in it:

- conversations and their messages,
- entities the messages mention (people, projects, technologies),
- facts linking the entities ("agdb_server is built on agdb"),
- the actions the agent took and what they touched.

In `agdb` each of these is a node or an edge with key-value properties. There is no schema to migrate when the agent starts noticing a new kind of thing, you simply store it. Recalling context for the next prompt is then a traversal: start at the entity mentioned in the new message and follow its relations to what it is connected to. Thanks to `agdb`'s storage design a lookup or a traversal does not get slower as the database grows, its cost depends only on what it visits (see [Why graph?](/blog/why-graph)). That matters when an agent recalls memory on every step of its loop.

```rs
// The facts within two hops of an entity. Following the outgoing edges only
// the facts are traversed and the distance condition also stops the search.
let facts = t.exec(
    QueryBuilder::select()
        .values("relation")
        .search()
        .from(entity)
        .where_()
        .edge()
        .and()
        .distance_less_than(4)
        .query(),
)?;
```

Knowledge can be traced back: the messages mentioning an entity are simply its neighbours pointing to it (and storing the source message on a fact edge traces each fact). Forgetting a conversation (e.g. when the user asks for it) removes its messages while keeping what was learned from them, or not, it is up to you. The complete runnable example is walked through in [Agent memory](/docs/examples/agent-memory) (`cargo run -p agdb --example agent_memory`).

## Queries an LLM cannot inject into

Text written by users, and text written by models, is untrusted. When it ends up concatenated into an SQL or Cypher string it can change what the query does. That is prompt injection meeting SQL injection.

`agdb` has [no query language](/blog/why-not-sql). Queries are typed objects built in your application's language and the user's or the model's text is only ever a value:

```rs
let message = Message {
    db_id: None,
    role: "user".to_string(),
    text: untrusted_text, // stored as is, never parsed as a query
};
db.exec_mut(QueryBuilder::insert().nodes().values([&message]).query())?;
```

There is nothing to escape and nothing to inject into. The same holds when the agent's tools are written by the model itself: an AI coding assistant writing `agdb` queries writes ordinary Rust, TypeScript, Python or PHP code with a typed query builder. A wrong query is most often a compile error (Rust) or a type-check error (TypeScript, or Python and PHP with a type checker such as mypy or PHPStan) rather than a query that silently does something else at runtime.

## One file per agent

The embedded `agdb` is a library and the whole database is a [single file](/blog/single-file). That makes some designs easy that are hard with a database server:

- a separate memory file per agent, user or session, deleted when it is no longer needed,
- local-first and on-device AI applications where the data never leaves the machine,
- shipping a prepared knowledge graph as one file together with the application.

When the agents need to share memory, the same queries run against [`agdb_server`](/docs/references/server) with users, roles, backups and a replicated cluster, and the [studio](/docs/guides/how-to-use-studio) lets you look at the agent's memory as a 3D graph.

## What agdb is not

`agdb` is not a vector database. It can store embeddings as vector values (`Vec<f64>`) but it has no similarity search index. For semantic search over documents, use a vector database and let `agdb` hold the knowledge graph around it: the relationships, provenance and permissions. A vector search returns the ids of the closest chunks and `agdb` expands from them to everything connected. That is the idea behind GraphRAG and the two kinds of databases complement each other.

## Documentation for AI assistants

The documentation of `agdb` is available to AI tools in plain markdown:

- [llms.txt](https://agdb.agnesoft.com/llms.txt) lists every page with a short description,
- [llms-full.txt](https://agdb.agnesoft.com/llms-full.txt) has the complete documentation in one file,
- every page has its markdown version under `/raw`, e.g. [/raw/docs/guides/quickstart.md](https://agdb.agnesoft.com/raw/docs/guides/quickstart.md).

Point your coding assistant at them, or add `llms-full.txt` to its context, and it will write `agdb` queries with the real API rather than guessing it.
