Skip to main content
The AXON Python API provides programmatic access to the compiler pipeline and runtime execution engine. This allows you to compile and execute AXON programs directly from Python, integrate AXON into larger applications, or build custom tooling.

Architecture

The AXON API is organized into three major phases:

Phase 1: Compilation Pipeline

from axon import Lexer, Parser, TypeChecker, IRGenerator

source = open("my_program.axon").read()

# Phase 1.1: Lexical analysis
lexer = Lexer(source, filename="my_program.axon")
tokens = lexer.tokenize()

# Phase 1.2: Parsing
parser = Parser(tokens)
ast = parser.parse()

# Phase 1.3: Type checking
checker = TypeChecker(ast)
errors = checker.check()

if errors:
    for error in errors:
        print(f"Error at line {error.line}: {error.message}")
    exit(1)

# Phase 1.4: IR generation
ir_gen = IRGenerator()
ir_program = ir_gen.generate(ast)

Phase 2: Backend Compilation

from axon import get_backend

# Select a backend
backend = get_backend("anthropic")

# Compile to provider-specific prompts
compiled = backend.compile_program(ir_program)

print(compiled.to_dict())

Phase 3: Runtime Execution

from axon.runtime import Executor, ModelClient
from anthropic import Anthropic

class AnthropicClient(ModelClient):
    def __init__(self, api_key: str):
        self.client = Anthropic(api_key=api_key)
    
    async def call(self, system_prompt: str, user_prompt: str, **kwargs):
        response = self.client.messages.create(
            model="claude-3-5-sonnet-20241022",
            system=system_prompt,
            messages=[{"role": "user", "content": user_prompt}]
        )
        return ModelResponse(content=response.content[0].text)

# Execute the compiled program
client = AnthropicClient(api_key="...")
executor = Executor(client=client)

result = await executor.execute(compiled)

for unit in result.unit_results:
    print(f"Flow: {unit.flow_name}")
    for step in unit.step_results:
        print(f"  {step.step_name}: {step.response.content}")

Quick Reference

Compiler API

ClassPurposeDocumentation
LexerTokenizes source codeLexer API
ParserBuilds abstract syntax treeParser API
TypeCheckerValidates epistemic typesTypeChecker API
IRGeneratorGenerates intermediate representationIR Generator API

Backend API

Function/ClassPurposeDocumentation
get_backend(name)Get a backend instanceBackends API
BACKEND_REGISTRYAvailable backend registryBackends API
BaseBackendAbstract backend interfaceBackends API

Runtime API

ClassPurposeDocumentation
ExecutorMain runtime orchestratorExecutor API
ContextManagerExecution state managementContext API
BaseToolTool implementation interfaceTools API
MemoryBackendSemantic memory storageMemory API

Installation

pip install axon-lang

Version

Current version: 0.4.0a0
import axon
print(axon.__version__)  # '0.4.0a0'

Next Steps

Lexer API

Tokenize AXON source code

Parser API

Build cognitive syntax trees

Executor API

Execute compiled programs

Tools API

Build custom runtime tools

Build docs developers (and LLMs) love