Embedded Python

Use Hexi as a Python library when you need custom orchestration around one-step runs.

Minimal embedded usage

from pathlib import Path

from hexi.adapters.events_console import ConsoleEventSink
from hexi.adapters.exec_local import LocalExec
from hexi.adapters.memory_file import FileMemory
from hexi.adapters.model_openrouter_http import OpenRouterHTTPModel
from hexi.adapters.workspace_local_git import LocalGitWorkspace
from hexi.core.service import RunStepService

workspace = LocalGitWorkspace(Path.cwd())
memory = FileMemory(workspace.repo_root())
memory.ensure_initialized()
config = memory.load_model_config()
memory.apply_api_key_to_env(config.provider)

service = RunStepService(
    model=OpenRouterHTTPModel(),
    workspace=workspace,
    executor=LocalExec(),
    events=ConsoleEventSink(),
    memory=memory,
)

result = service.run_once("Add one focused unit test")
print(result.success)

When to embed

  • you need application-specific event sinks,
  • you want tighter orchestration around run lifecycle,
  • you want to compose Hexi with existing Python workflows.