tinyagents 1.3.0

A recursive language-model (RLM) harness for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# Harness Embeddings And Retrieval Feature

Embeddings are provider-neutral vector representations used for retrieval,
semantic search, deduplication, document compression, reranking, and
retrieval-augmented prompt context. This feature owns embedding models, vector
stores, retrievers, indexing records, retrieval events, and deterministic test
utilities.

## Source Inspiration

LangChain keeps embeddings and retrieval as core primitives rather than
embedding them inside the chat model abstraction:

- embedding interface:
  <https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/embeddings/embeddings.py>
- fake and deterministic fake embeddings:
  <https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/embeddings/fake.py>
- vector store interface and in-memory vector store:
  <https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/vectorstores/base.py>
  and
  <https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/vectorstores/in_memory.py>
- retriever interface:
  <https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/retrievers.py>
- indexing API and record managers:
  <https://github.com/langchain-ai/langchain/tree/master/libs/core/langchain_core/indexing>
- vector math utilities for cosine similarity and maximal marginal relevance:
  <https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/vectorstores/utils.py>
- partner vector stores and embedding providers such as Chroma, Qdrant, OpenAI,
  Ollama, and others:
  <https://github.com/langchain-ai/langchain/tree/master/libs/partners>

TinyAgents should follow the separation: chat models generate messages,
embedding models generate vectors, vector stores search vectors, retrievers
return documents, and prompt/middleware code decides what retrieved context
enters a model request.

## Responsibilities

- Define provider-neutral embedding traits.
- Register named embedding providers.
- Embed documents and queries separately.
- Support batch embedding and async-native embedding.
- Track vector dimensionality and distance metric.
- Normalize provider metadata, usage, cost, and errors.
- Cache embeddings when policy allows.
- Store and query dense vectors.
- Support sparse vectors and hybrid retrieval where backends support them.
- Support similarity search, score-threshold search, and maximal marginal
  relevance search.
- Support metadata filters.
- Support add, update, delete, get-by-id, and search operations.
- Support incremental indexing with content hashes and record managers.
- Expose retrievers to middleware, tools, and prompt assembly.
- Emit embedding, indexing, vector-store, and retriever events.
- Provide deterministic fake embeddings and in-memory vector stores for tests.

## Non-Responsibilities

- It does not decide graph routing.
- It does not automatically inject retrieved documents into every prompt.
- It does not replace durable application stores.
- It does not require a vector store for every embedding call.
- It does not assume query embeddings and document embeddings are identical.
- It does not hide backend-specific vector-store capabilities when users need
  them.

## Package Shape

Target layout:

```text
src/harness/embeddings.rs
```

The first implementation can keep the feature in one module. If it grows large,
split it into:

```text
src/harness/embeddings/
  mod.rs
  embedding.rs
  vector_store.rs
  retriever.rs
  indexing.rs
  sparse.rs
  testkit.rs
```

## Core Types

```rust
#[async_trait]
pub trait EmbeddingModel<Ctx = ()>: Send + Sync {
    fn profile(&self) -> Option<&EmbeddingProfile>;

    async fn embed_documents(
        &self,
        ctx: &mut RunContext<Ctx>,
        request: EmbedDocumentsRequest,
    ) -> Result<EmbedDocumentsResponse>;

    async fn embed_query(
        &self,
        ctx: &mut RunContext<Ctx>,
        request: EmbedQueryRequest,
    ) -> Result<EmbedQueryResponse>;
}

pub struct EmbeddingRegistry<Ctx = ()> {
    models: HashMap<EmbeddingName, Arc<dyn EmbeddingModel<Ctx>>>,
    default: Option<EmbeddingName>,
}
```

The trait separates document embeddings from query embeddings because some
providers optimize them differently. Even when the current provider uses the
same endpoint for both, the public contract should preserve the distinction.

## Requests And Responses

```rust
pub struct EmbedDocumentsRequest {
    pub model: EmbeddingName,
    pub texts: Vec<String>,
    pub input_type: EmbeddingInputType,
    pub dimensions: Option<usize>,
    pub provider_options: serde_json::Value,
    pub cache_policy: Option<CachePolicy>,
    pub metadata: serde_json::Value,
}

pub struct EmbedDocumentsResponse {
    pub vectors: Vec<EmbeddingVector>,
    pub usage: Option<UsageRecord>,
    pub provider: ProviderMetadata,
    pub cache: Option<CacheDecision>,
}

pub struct EmbeddingVector {
    pub values: Vec<f32>,
    pub dimension: usize,
    pub norm: Option<f32>,
}
```

`Vec<f32>` should be the default in-memory representation. Provider adapters may
receive or store `f64`, quantized, binary, or backend-native vectors, but the
harness should normalize the common path to a predictable Rust type.

## Embedding Profiles

```rust
pub struct EmbeddingProfile {
    pub provider: ProviderName,
    pub model: EmbeddingName,
    pub dimensions: Option<usize>,
    pub allowed_dimensions: Vec<usize>,
    pub max_batch_size: Option<usize>,
    pub max_input_tokens: Option<usize>,
    pub supports_documents: bool,
    pub supports_queries: bool,
    pub supports_sparse: bool,
    pub supports_image_input: bool,
    pub distance_metrics: Vec<DistanceMetric>,
    pub provider_extras: serde_json::Value,
}
```

Profiles are used to:

- validate requested dimensions
- choose batch sizes
- enforce provider input limits
- choose dense, sparse, or hybrid search paths
- report retriever provenance in events
- reject incompatible vector-store/index configurations early

## Vector Stores

```rust
#[async_trait]
pub trait VectorStore<Ctx = ()>: Send + Sync {
    async fn add_documents(
        &self,
        ctx: &mut RunContext<Ctx>,
        documents: Vec<IndexedDocument>,
        options: AddDocumentsOptions,
    ) -> Result<Vec<DocumentId>>;

    async fn delete(&self, ctx: &mut RunContext<Ctx>, ids: Vec<DocumentId>) -> Result<DeleteResult>;

    async fn get_by_ids(
        &self,
        ctx: &mut RunContext<Ctx>,
        ids: Vec<DocumentId>,
    ) -> Result<Vec<IndexedDocument>>;

    async fn search(
        &self,
        ctx: &mut RunContext<Ctx>,
        request: VectorSearchRequest,
    ) -> Result<Vec<ScoredDocument>>;
}
```

Search types:

- `similarity`: return nearest documents
- `similarity_with_score`: include backend score or normalized relevance score
- `similarity_score_threshold`: filter results below a configured relevance
  threshold
- `mmr`: maximal marginal relevance, balancing query similarity and diversity
- `by_vector`: search with a caller-provided vector
- `hybrid`: combine dense and sparse retrieval where supported

The store must document score semantics. Some backends return distance where
lower is better; others return similarity where higher is better. TinyAgents
should normalize relevance scores when possible and preserve raw backend scores
for auditability.

## Retrievers

Retrievers are query-to-document components. They are more general than vector
stores because they can wrap vector stores, keyword search, hybrid search,
rerankers, document compressors, or application-specific logic.

```rust
#[async_trait]
pub trait Retriever<Ctx = ()>: Send + Sync {
    async fn retrieve(
        &self,
        ctx: &mut RunContext<Ctx>,
        request: RetrievalRequest,
    ) -> Result<Vec<ScoredDocument>>;
}
```

Retriever requests should carry:

- query text
- optional query vector
- search type
- `k`
- `fetch_k`
- score threshold
- MMR lambda
- metadata filter
- tags and metadata

Retrievers should emit `retriever.started`, `retriever.completed`, and
`retriever.failed` events. Events should include retriever name, embedding
provider/model, vector-store provider, search type, result count, timings, and
redacted query metadata.

## Indexed Documents

```rust
pub struct IndexedDocument {
    pub id: Option<DocumentId>,
    pub text: String,
    pub metadata: serde_json::Value,
    pub source_id: Option<String>,
    pub embedding: Option<EmbeddingVector>,
}
```

Document ids matter for deletion, updates, deduplication, and provenance. When
ids are not supplied, stores may generate ids, but indexing workflows should
prefer deterministic ids derived from source identity or content hashes.

## Indexing And Record Managers

LangChain's indexing layer hashes document content and metadata, stores record
manager entries, and avoids re-indexing unchanged documents. TinyAgents should
make that pattern explicit.

```rust
#[async_trait]
pub trait RecordManager: Send + Sync {
    async fn get_time(&self) -> Result<SystemTime>;
    async fn update(&self, records: Vec<IndexRecord>) -> Result<()>;
    async fn exists(&self, keys: Vec<IndexKey>) -> Result<Vec<bool>>;
    async fn list_keys(&self, filter: RecordFilter) -> Result<Vec<IndexKey>>;
    async fn delete_keys(&self, keys: Vec<IndexKey>) -> Result<()>;
}

pub struct IndexPolicy {
    pub key_encoder: KeyEncoder,
    pub cleanup: CleanupPolicy,
    pub batch_size: usize,
    pub force_update: bool,
}
```

Indexing should support:

- deterministic content and metadata hashing
- configurable hash algorithms
- source id grouping
- deduplication while preserving order
- incremental indexing
- full cleanup
- scoped cleanup by source id
- monotonic server time checks where the backend supports them
- failure handling when vector-store writes and record-manager writes diverge

SHA-1-style defaults should be documented as compatibility-oriented rather than
collision-resistant. New TinyAgents implementations should prefer `sha256`,
`sha512`, `blake3`, or caller-provided encoders.

## Sparse And Hybrid Retrieval

Qdrant and other backends support sparse vectors and hybrid dense+sparse
retrieval. TinyAgents should not force sparse support into the dense embedding
trait. Use separate types:

```rust
pub struct SparseVector {
    pub indices: Vec<u32>,
    pub values: Vec<f32>,
}

#[async_trait]
pub trait SparseEmbeddingModel<Ctx = ()>: Send + Sync {
    async fn embed_sparse_documents(
        &self,
        ctx: &mut RunContext<Ctx>,
        texts: Vec<String>,
    ) -> Result<Vec<SparseVector>>;

    async fn embed_sparse_query(
        &self,
        ctx: &mut RunContext<Ctx>,
        text: String,
    ) -> Result<SparseVector>;
}
```

Hybrid search should record the dense model, sparse model, fusion strategy, and
backend-specific scoring metadata.

## Caching

Embedding cache keys should include:

- provider
- model
- input text
- input type: document or query
- requested dimensions
- provider options
- preprocessing version
- tokenizer/chunker version when applicable

Cached embeddings should record vector dimension, provider metadata, and
creation time. Cache hits should still emit usage/cache events, but should not
pretend provider tokens were consumed.

## Usage And Cost

Embedding providers may bill by input tokens, characters, requests, or batches.
Usage records should support:

- input tokens
- input characters
- number of texts
- number of vectors
- dimensions
- batch count
- provider-specific extras

Cost records should support per-token, per-character, per-vector, and per-request
pricing. Pricing data must be updateable outside provider adapters.

## Events

Event kinds:

- `embedding.started`
- `embedding.completed`
- `embedding.failed`
- `vector_store.added`
- `vector_store.deleted`
- `vector_store.searched`
- `retriever.started`
- `retriever.completed`
- `retriever.failed`
- `indexing.started`
- `indexing.batch_completed`
- `indexing.completed`
- `indexing.failed`

Events should include run ids, component ids, provider/model names, vector-store
namespace, batch sizes, result counts, timing, usage, cost, and redacted query or
document fingerprints.

## Retrieval Context Assembly

Retrievers return candidate documents. Prompt and middleware code decide how to
use them. A retrieval middleware may:

- run one or more retrievers before a model call
- deduplicate documents by source id or content hash
- rerank or compress results
- drop documents when context pressure is high
- summarize large documents
- attach citation metadata to context blocks
- emit provenance events for every injected context block

The harness must preserve provenance so model outputs can cite retrieved
sources, and so tests can assert which documents entered the prompt.

## Built-In Implementations

Initial implementations:

- `DeterministicFakeEmbedding` for tests
- `RandomFakeEmbedding` for shape tests only
- `InMemoryVectorStore` for examples and unit tests
- `InMemoryRecordManager` for indexing tests

Feature-gated future adapters:

- `provider-openai-embeddings`
- `provider-ollama-embeddings`
- `vector-chroma`
- `vector-qdrant`
- `vector-pgvector`
- `vector-mongodb-atlas`
- `vector-redis`

## Conformance Tests

Embedding providers should pass tests for:

- document embedding count matches input count
- query embedding returns one vector
- dimensions match profile or requested dimension
- empty input behavior is documented
- Unicode input
- batch sizing
- async behavior
- cache hit/miss behavior
- usage and cost records
- cancellation and timeout
- provider error classification

Vector stores should pass tests for:

- add documents
- add texts
- generated ids
- caller-supplied ids
- delete by id
- get by ids without assuming return order
- similarity search
- similarity search with score
- score-threshold search
- MMR search
- metadata filters
- update replaces vector when text changes
- persistence behavior where supported

Indexing should pass tests for:

- deterministic hashes
- metadata hash changes
- deduplication
- incremental no-op on unchanged documents
- cleanup of stale documents
- record-manager/vector-store failure handling