The ContextManager maintains mutable execution state that flows between steps within a single execution unit. It’s the working memory of a running AXON program.
from axon.runtime import ContextManagerctx = ContextManager( system_prompt="You are a legal expert.", tracer=tracer)# Store step resultsctx.set_step_result("extract", {"clauses": [1, 2, 3]})# Retrieve for downstream stepsresult = ctx.get_step_result("extract")print(result) # {"clauses": [1, 2, 3]}# Track conversationctx.append_message("user", "Analyze this contract")ctx.append_message("assistant", "I found 3 clauses...")print(f"Message history: {ctx.message_count} messages")
from axon.runtime import ContextManagerfrom axon.runtime.tracer import Tracertracer = Tracer(program_name="MyProgram", backend_name="anthropic")ctx = ContextManager( system_prompt="You are an AI assistant.", tracer=tracer)
# Store flow parametersctx.set_variable("document", contract_text)ctx.set_variable("max_risk", 0.8)# Store intermediate valuesctx.set_variable("temp_clauses", extracted_clauses)
ctx.append_message("user", "Analyze this contract")ctx.append_message("assistant", "I found 5 key clauses...")ctx.append_message("user", "What are the risks?")ctx.append_message("assistant", "The main risks are...")
history = ctx.get_message_history()for msg in history: print(f"{msg['role']}: {msg['content'][:50]}...")# Output:# user: Analyze this contract...# assistant: I found 5 key clauses...# user: What are the risks?...# assistant: The main risks are...
ctx = ContextManager(system_prompt="You are an expert analyst.")# Turn 1ctx.append_message("user", "What are the key clauses?")response1 = await model.call( system_prompt=ctx.system_prompt, user_prompt="What are the key clauses?")ctx.append_message("assistant", response1.content)# Turn 2 (with history)ctx.append_message("user", "Are there any risks?")response2 = await model.call( system_prompt=ctx.system_prompt, user_prompt="Are there any risks?", conversation_history=ctx.get_message_history())ctx.append_message("assistant", response2.content)
import jsonctx = ContextManager(system_prompt="...")# Capture state at key pointssnapshots = []for step in flow.steps: ctx.current_step = step.name result = await execute_step(step) ctx.set_step_result(step.name, result) # Capture snapshot after each step snapshot = ctx.snapshot() snapshots.append(snapshot)# Save all snapshots for debuggingwith open("debug_trace.json", "w") as f: json.dump([s.to_dict() for s in snapshots], f, indent=2)