Skip to content

Reuse an analysis

Keep an analysis open when you want to compare budgets or focus queries over the same source snapshot. Reusing only an engine retains its parsers and tokenizer, while each run call still scans and analyzes the inputs again.

Python
from dataclasses import replace
from contextcraft import Config, Engine

config = Config(budget_tokens=8000)
with Engine(config) as engine:
    with engine.analyze_path(".") as analysis:
        artifact = engine.generate(analysis)
        focused = engine.generate(analysis, replace(config, focus_query="refresh token"))
        metadata = analysis.metadata()

Configuration is an immutable dataclass, so use replace for a changed request. Discovery settings apply when analysis is created, while generation settings can change between calls using that snapshot.

Inspect files and dependencies

analysis.metadata() exposes files, dependency edges, resolution outcomes, and project count. IDs belong to one analysis and connect file metadata to artifact scores and selection. Metadata paths are raw analysis data and do not receive artifact path redaction.

Findings contain source or path ranges and rule IDs without matched values. Secret protection applies during generation by default, while raw analysis retains original source.

Close native handles

Each engine serializes its calls, including closure, while separate engines can run concurrently. Analyses own their memory independently of an engine, and closed handles raise ClosedHandleError. Create new handles after a fork.

Invalid settings raise ValueError, filesystem errors raise OSError, and allocation failures raise MemoryError. Block mode can raise SecretsDetectedError when findings prevent generation. Preserve the complete exception message when diagnosing a failure.

See the Python API reference for exported classes and types, and configuration for defaults.

Continue with in-memory files to analyze editor buffers or source supplied by another tool.