Skip to content

API Reference

code-assist exposes a Python API for programmatic use. The primary entry point is QueryEngine, which orchestrates the full agent loop — system prompt assembly, tool filtering, API streaming, and tool execution.

Quick Start

python
import asyncio
from code_assist.core.query_engine import QueryEngine, QueryEngineConfig
from code_assist.core.query import TextEvent, ToolUseEvent, ToolResultEvent, DoneEvent
from code_assist.tools.registry import get_all_tools


async def main():
    # Configure the engine
    config = QueryEngineConfig(
        cwd="/path/to/project",
        project_root="/path/to/project",
        tools=get_all_tools(),
        model="claude-sonnet-4-6",
        max_turns=50,
        max_tokens=16384,
        api_key="sk-ant-...",  # or set ANTHROPIC_API_KEY env var
    )

    engine = QueryEngine(config)

    # Submit a message and stream events
    async for event in engine.submit_message("Explain the architecture of this project"):
        if isinstance(event, TextEvent):
            print(event.text, end="", flush=True)
        elif isinstance(event, ToolUseEvent):
            print(f"\n[Tool: {event.tool_name}]")
        elif isinstance(event, ToolResultEvent):
            if event.is_error:
                print(f"\n[Error: {event.result}]")
        elif isinstance(event, DoneEvent):
            print(f"\n\n--- Done ({event.total_turns} turns) ---")


asyncio.run(main())

Core Modules

ModuleImport PathDescription
QueryEnginecode_assist.core.query_engineHigh-level orchestrator — the primary API
query()code_assist.core.queryLow-level agent loop (async generator)
Tool Protocolcode_assist.tools.baseTool protocol and ToolDef base class
Tool Registrycode_assist.tools.registryget_all_tools() to assemble tools
Settingscode_assist.config.settingsload_merged_settings() for configuration
CLAUDE.mdcode_assist.config.claude_mdMemory file discovery and context building
Memorycode_assist.memory.memory_scanStructured memory entry scanning
Typescode_assist.types.*Message, permission, hook, command, plugin types

Event Types

The QueryEngine.submit_message() method yields these event types:

EventFieldsDescription
TextEventtext: strStreamed text from the assistant
ToolUseEventtool_use_id, tool_name, tool_inputTool call initiated by the model
ToolResultEventtool_use_id, result, is_errorResult of a tool execution
AssistantMessageEventmessage: AssistantMessageComplete assistant message (after streaming)
ErrorEventerror: str, is_retryable: boolAPI or execution error
DoneEventstop_reason, total_turns, total_cost_usdQuery loop completed

Multi-Turn Conversations

The QueryEngine maintains conversation history across calls:

python
engine = QueryEngine(config)

# First turn
async for event in engine.submit_message("Read the README"):
    ...

# Second turn — conversation history is preserved
async for event in engine.submit_message("Now summarize it in bullet points"):
    ...

# Check state
print(f"Total turns: {engine.total_turns}")
print(f"Message count: {len(engine.messages)}")

# Reset
engine.clear_messages()

Headless / CI Usage

For non-interactive use, configure the engine with explicit API key and restricted tools:

python
from code_assist.tools.base import ToolDef
from code_assist.tools.file_read.file_read_tool import FileReadTool
from code_assist.tools.glob_tool.glob_tool import GlobTool
from code_assist.tools.grep_tool.grep_tool import GrepTool

config = QueryEngineConfig(
    cwd="/path/to/project",
    project_root="/path/to/project",
    tools=[FileReadTool(), GlobTool(), GrepTool()],  # Read-only tools only
    model="claude-sonnet-4-6",
    max_turns=10,
    api_key=os.environ["ANTHROPIC_API_KEY"],
    custom_system_prompt="You are a code reviewer. Only analyze, never modify.",
)

Further Reading

  • QueryEngine — full class reference and configuration options.
  • Tools API — building custom tools with the Tool protocol and ToolDef base class.

Research and educational use only. Inspired by Claude Code by Anthropic. All original rights reserved by Anthropic.