Heuristic:0xranx OpenContext Index Build Cost Governance
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Cost_Management |
| Last Updated | 2026-10-24 10:00 GMT |
Overview
Index builds incur external embedding API costs and should not be auto-triggered; background sync uses 5-minute batch intervals to minimize API calls.
Description
OpenContext's index build process calls an external embedding API for every document chunk, which incurs per-token costs. The system is designed to avoid automatic index builds — users must explicitly run `oc index build`. The background `IndexSyncService` batches document change events and processes them every 5 minutes (300 seconds) by default, rather than re-indexing on every file change. This reduces API calls and rate limit pressure. The sync service also skips processing if no index has been built yet.
Usage
Use this heuristic when managing index build costs. Never auto-trigger `oc index build` without user approval. For incremental updates, rely on the `IndexSyncService` which batches changes. If cost is a concern, increase the sync interval or disable sync temporarily.
The Insight (Rule of Thumb)
- Action: Never auto-trigger index builds. Use the 5-minute batched `IndexSyncService` for incremental updates.
- Value: `check_interval_secs = 300` (5 minutes), `batch_size = 10` (documents per embedding batch)
- Trade-off: Longer intervals = fewer API calls but longer staleness window. Shorter intervals = more responsive but more expensive.
- Compatibility: The sync service skips updates if no index exists (won't auto-create an index).
Reasoning
The cost governance design follows several principles:
- Explicit builds: The `AGENTS.md` instructions explicitly state "Index builds (`oc index build`) may incur external embedding cost; do not auto-trigger by default—ask for approval or let the platform handle it."
- Batched sync: The `IndexSyncService` collects all file change events into a `HashMap` keyed by file path, then processes them in one batch. Multiple changes to the same file are deduplicated — only the latest action survives.
- Skip if no index: The sync service checks `index_exists()` before processing, so it won't silently create an index and incur costs.
- First tick delay: The interval timer starts after the first interval (not immediately), avoiding a burst of processing on startup.
Code Evidence
AGENTS.md guidance from `AGENTS.md`:
Index builds (`oc index build`) may incur external embedding cost;
do not auto-trigger by default—ask for approval or let the platform handle it.
Default 5-minute interval from `crates/opencontext-core/src/search/index_sync.rs:47-55`:
pub fn new(config: SearchConfig, contexts_root: PathBuf) -> Self {
Self {
// ...
check_interval_secs: 300, // 5 minutes
}
}
Batch deduplication from `index_sync.rs:118-135`:
for action in actions {
match &action {
IndexAction::Update { rel_path } => {
pending_guard.insert(rel_path.clone(), action);
}
IndexAction::Remove { rel_path } => {
pending_guard.insert(rel_path.clone(), action);
}
IndexAction::Rename { old_path, new_path } => {
pending_guard.remove(old_path);
pending_guard.insert(new_path.clone(), action);
}
}
}
Skip if no index from `index_sync.rs:218-220`:
if !indexer.index_exists().await {
log::debug!("[IndexSync] Index not built, skipping updates");
continue;
}
First tick delay from `index_sync.rs:192-194`:
// Start first tick after interval_secs (not immediately)
let start = Instant::now() + Duration::from_secs(interval_secs);
let mut ticker = interval_at(start, Duration::from_secs(interval_secs));
Node.js sync default from `src/core/search/indexSync.js:22-24`:
async function start(options = {}) {
const intervalSecs = options.intervalSecs ?? 300; // Default 5 minutes
return await native.get().startIndexSync(intervalSecs);
}