Python API

CodeSight as a library - call it from Python code.

Basic Usage

Python
import asyncio
from codesight.config import load_config
from codesight.analyzer import Analyzer, TaskType

config = load_config()
analyzer = Analyzer(config, provider_name="openai")

result = asyncio.run(analyzer.analyze_file(
    "src/auth.py",
    TaskType.SECURITY,
))

print(result.content)
print(f"Tokens: {result.tokens_used}")
print(f"Model: {result.model}")

Analyzer

codesight.analyzer
class Analyzer:
    def __init__(config: AppConfig, provider_name: str | None = None)
    async def analyze_file(file_path: str, task: TaskType, extra_context: str | None = None) -> AnalysisResult
    async def analyze_files(file_paths: list[str], task: TaskType) -> list[AnalysisResult]
    async def health() -> bool

TaskType

codesight.analyzer
class TaskType(Enum):
    REVIEW = "review"
    BUGS = "bugs"
    SECURITY = "security"
    DOCS = "docs"
    EXPLAIN = "explain"
    REFACTOR = "refactor"

AnalysisResult

codesight.analyzer
@dataclass
class AnalysisResult:
    task: TaskType
    file_path: str
    content: str       # markdown analysis output
    model: str         # model used (e.g. "gpt-5.4")
    provider: str      # provider name (e.g. "OpenAI")
    tokens_used: int   # total tokens (prompt + completion)
    usage: dict        # {"prompt_tokens": N, "completion_tokens": N}

Configuration

Python
from codesight.config import AppConfig, ProviderConfig, load_config, save_config

config = AppConfig(
    default_provider="openai",
    providers={
        "openai": ProviderConfig(
            provider="openai",
            api_key="sk-...",
            model="gpt-5.4",
        ),
    },
)

save_config(config)

Compression

Python
from codesight.compression import build_code_map, compress_for_prompt

code_map = build_code_map("src/main.py")
print(f"Language: {code_map.language}")
print(f"Symbols: {code_map.symbols}")
print(f"Compression: {code_map.ratio:.0%}")

compressed = compress_for_prompt("src/main.py", source_code)
print(compressed)

Templates

Python
from codesight.templates import list_templates, get_template, save_template

templates = list_templates()
for name, t in templates.items():
    print(f"{name}: {t['description']}")

save_template(
    "my-audit",
    "My Security Audit",
    "Custom security check for Django apps",
    "You are auditing a Django application. Focus on...",
)

Streaming

Python
import asyncio
from codesight.config import load_config
from codesight.streaming import stream_analysis

config = load_config()
messages = [
    {"role": "system", "content": "Review this code."},
    {"role": "user", "content": "def hello(): pass"},
]

async def main():
    async for chunk in stream_analysis(config, messages):
        print(chunk, end="", flush=True)

asyncio.run(main())

Cost Estimation

Python
from codesight.cost import estimate_cost, format_cost

cost = estimate_cost("gpt-5.4", prompt_tokens=800, completion_tokens=400)
print(format_cost(cost))