The `KB DOCUMENTS COUNT` keyword returns the total number of documents stored in the bot's knowledge base.
---
## Syntax
```basic
count = KB DOCUMENTS COUNT
```
---
## Parameters
None. Returns the count for the current bot's knowledge base.
---
## Description
`KB DOCUMENTS COUNT` queries the database to return the total number of documents that have been indexed in the bot's knowledge base. This is a lightweight operation compared to `KB STATISTICS` when you only need the document count.
Use cases include:
- Checking if knowledge base has content
- Displaying document counts in conversations
- Conditional logic based on KB size
- Simple monitoring and logging
---
## Return Value
Returns an integer representing the total document count. Returns `0` if no documents exist or if an error occurs.
---
## Examples
### Basic Count Check
```basic
' Check how many documents are in KB
doc_count = KB DOCUMENTS COUNT
TALK "The knowledge base contains " + doc_count + " documents."
```
### Conditional KB Usage
```basic
' Only use KB if it has content
doc_count = KB DOCUMENTS COUNT
IF doc_count > 0 THEN
USE KB
answer = SEARCH user_question
TALK answer
ELSE
TALK "The knowledge base is empty. Please add some documents first."
- [KB STORAGE SIZE](keyword-kb-storage-size.md) — Get storage usage
- [KB LIST COLLECTIONS](keyword-kb-list-collections.md) — List all collections
- [CLEAR KB](keyword-clear-kb.md) — Clear knowledge base
- [USE KB](keyword-use-kb.md) — Enable KB for queries
---
## Implementation Notes
- Implemented in Rust under `src/basic/keywords/kb_statistics.rs`
- Queries PostgreSQL `kb_documents` table
- Filters by current bot ID
- Returns 0 on error (does not throw)
- Very fast operation (single COUNT query)
---
## Summary
`KB DOCUMENTS COUNT` provides a quick way to get the total number of documents in the knowledge base. Use it for simple checks, conditional logic, and lightweight monitoring. For more detailed statistics, use `KB STATISTICS` instead.