MACPRunner¶
The execution engine for running agents on a graph.
Basic Usage¶
from gmas.execution import MACPRunner
# Define LLM caller
def llm_caller(prompt: str) -> str:
# Your LLM call here
return response
# Create runner
runner = MACPRunner(llm_caller=llm_caller)
# Execute
result = runner.run_round(graph)
print(result.final_answer)
print(result.execution_order)
print(result.total_time)
Configuration¶
from gmas.execution import MACPRunner, RunnerConfig
config = RunnerConfig(
enable_parallel=True,
max_parallel_size=3,
enable_memory=True,
memory_context_limit=5,
)
runner = MACPRunner(
llm_caller=llm_caller,
config=config,
)
Streaming¶
from gmas.execution import StreamEventType
for event in runner.stream(graph):
if event.event_type == StreamEventType.RUN_START:
print("Execution started")
elif event.event_type == StreamEventType.AGENT_START:
print(f"{event.agent_name} started")
elif event.event_type == StreamEventType.AGENT_OUTPUT:
print(f"{event.agent_name}: {event.content}")
elif event.event_type == StreamEventType.RUN_END:
print(f"Done in {event.total_time:.2f}s")
Async Execution¶
import asyncio
async def async_llm(prompt: str) -> str:
# Your async LLM call
return await async_response
runner = MACPRunner(async_llm_caller=async_llm)
result = await runner.arun_round(graph)
# Or async streaming
async for event in runner.astream(graph):
print(format_event(event))
Multi-Model Support¶
from gmas.execution import MACPRunner, LLMCallerFactory
# Create factory
factory = LLMCallerFactory()
# Register different callers
factory.register("gpt-4", my_gpt4_caller)
factory.register("claude", my_claude_caller)
factory.register("local", my_local_llm_caller)
# Use with runner
runner = MACPRunner(llm_factory=factory)
Budget Control¶
from gmas.execution import MACPRunner, BudgetConfig, RunnerConfig
budget = BudgetConfig(
total_token_limit=10000,
max_prompt_length=4000,
time_limit_seconds=300,
)
config = RunnerConfig(budget_config=budget)
runner = MACPRunner(llm_caller=llm_caller, config=config)
Memory¶
from gmas.execution import MACPRunner, MemoryConfig, RunnerConfig
memory_config = MemoryConfig(
working_max_entries=10,
long_term_max_entries=50,
)
config = RunnerConfig(
enable_memory=True,
memory_config=memory_config,
memory_context_limit=3, # Include last 3 in prompt
)
runner = MACPRunner(llm_caller=llm_caller, config=config)
# Access memory after execution
agent_memory = runner.get_agent_memory("agent_id")
Error Handling¶
from gmas.execution import MACPRunner, ErrorPolicy, RunnerConfig
config = RunnerConfig(
error_policy=ErrorPolicy.CONTINUE, # or RAISE, RETRY
max_retries=3,
)
runner = MACPRunner(llm_caller=llm_caller, config=config)
Structured Prompts¶
For an OpenAI-compatible structured caller: