> ## Documentation Index
> Fetch the complete documentation index at: https://autonomy.computer/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Knowledge bases

> Give agents the ability to search a corpus of documents.

The **Knowledge** class gives agents the ability to search a corpus of documents
for information beyond their model's training data and cut-off date. Instead of
hardcoding information in prompts or relying solely on their model's training set,
agents can dynamically retrieve the most relevant and latest information.

The **Knowledge** class uses vector embeddings to find the most relevant
document chunks based on semantic similarity.

***

Create a searchable knowledge base and give an agent access to it as a tool:

```python theme={null}
from autonomy import Agent, Knowledge, KnowledgeTool, Model, Node

async def main(node):
  # Create searchable knowledge base
  knowledge = Knowledge(
    name="legal_statutes",
    searchable=True,
    model=Model("embed-english-v3"),  # Embedding model
    max_results=5,  # Top 5 most relevant chunks
    max_distance=0.3  # Similarity threshold (0.0 = exact match, 1.0 = very different)
  )
  
  # Add legal documents
  await knowledge.add_text(
    document_name="contract_law",
    text="""
    Contract Formation Requirements:
    1. Offer: Clear proposal of terms
    2. Acceptance: Unequivocal agreement to terms
    3. Consideration: Exchange of value between parties
    4. Capacity: Legal ability to enter contract
    5. Legality: Lawful purpose and terms
    """
  )
  
  await knowledge.add_text(
    document_name="tort_law",
    text="""
    Elements of Negligence:
    - Duty of care owed to plaintiff
    - Breach of that duty
    - Causation (actual and proximate)
    - Damages suffered by plaintiff
    """
  )
  
  # Give agent access to the knowledge base as a tool
  await Agent.start(
    node=node,
    name="henry",
    instructions="""
    You are Henry, an expert legal assistant.
    Use search_statutes tool to find relevant legal information.
    """,
    model=Model("claude-sonnet-4-v1"),
    tools=[
      KnowledgeTool(knowledge=knowledge, name="search_statutes")
    ]
  )

Node.start(main)
```

***

## Adding Documents

### From Text

Add text directly to the knowledge base:

```python theme={null}
await knowledge.add_text(
  document_name="statute_330",
  text="# 15 USC 330: Weather Modification\n\nAny person engaging in weather modification activities..."
)
```

### From URLs

Load documents from the web:

```python theme={null}
await knowledge.add_document(
  document_name="sec_330",
  document_url="https://raw.githubusercontent.com/AlextheYounga/us-federal-code/refs/heads/master/usc/title-15-commerce-and-trade/chapter-9a-weather-modification-activities-or-attempts%3B-reporting-requirement/sec-330.md",
  content_type="text/markdown"  # Optional, auto-detected if omitted
)
```

Supported formats (via text extraction):

* Plain text
* Markdown
* HTML
* PDF (if markitdown is installed)
* Word documents (if markitdown is installed)
* Many more formats via markitdown

***

## Configuration Options

```python theme={null}
knowledge = Knowledge(
  name="docs",                     # Unique name for this knowledge base
  searchable=True,                 # Enable vector search
  model=Model("embed-english-v3"), # Model for generating embeddings
  max_results=10,                  # Max search results to return
  max_distance=0.2,                # Max cosine distance (0.0-1.0, lower = more similar)
  max_knowledge_size=4096,         # Max characters to include in context
  chunker=NaiveChunker(            # How to split documents
    max_characters=256,            # Chunk size
    overlap=16                     # Overlap between chunks
  )
)
```

**Key parameters:**

* **`model`** - Embedding model for vector search. Default: `embed-english-v3`
* **`max_results`** - Number of top results to return. Default: 10
* **`max_distance`** - Similarity threshold (0.0 = identical, 1.0 = completely different). Default: 0.2
* **`max_knowledge_size`** - Maximum characters in the returned context. Default: 4096
* **`chunker`** - Strategy for splitting documents into chunks

***

## Storage Backends

### In-Memory Storage (Default)

Stores everything in memory. Simple but not persistent:

```python theme={null}
from autonomy import InMemory, Knowledge

knowledge = Knowledge(name="docs", searchable=True, storage=InMemory())
```

### Database Storage

For persistent storage across restarts:

```python theme={null}
from autonomy import Database, Knowledge

# Initialize database connection
db = await Database.from_environment()
knowledge = Knowledge(name="docs", searchable=True, storage=db)
```

***

## Use Knowledge as a Tool

Give agents explicit control over when to search:

```python theme={null}
from autonomy import KnowledgeTool

await Agent.start(
  node=node,
  name="henry",
  instructions="""
  You are Henry, an expert legal assistant.
  Use search_statutes when you need to reference legal code.
  """,
  model=Model("claude-sonnet-4-v1"),
  tools=[KnowledgeTool(knowledge=knowledge, name="search_statutes")]
)
```
