import asyncio
from afk.agents import Agent, FailSafeConfig
from afk.core import Runner
agent = Agent(
name="analyst",
model="gpt-5.2",
instructions="Provide detailed analysis.",
fail_safe=FailSafeConfig(
max_total_cost_usd=1.00,
max_steps=20,
),
)
async def monitor_cost():
runner = Runner()
handle = await runner.run_stream(
agent, user_message="Provide a comprehensive analysis of Python async patterns"
)
step_count = 0
async for event in handle:
match event.type:
case "text_delta":
print(event.text_delta, end="", flush=True)
case "step_started" if event.step is not None:
step_count = event.step
case "tool_completed":
print(f"\n [STEP] Step {step_count} | Tool: {event.tool_name}")
case "completed" if event.result is not None:
usage = event.result.usage
print(f"\n\n--- Cost Summary ---")
print(f"State: {event.result.state}")
print(f"Tokens: {usage.total_tokens}")
print(f"Cost: ${usage.estimated_cost_usd:.4f}")
print(f"LLM calls: {usage.llm_call_count}")
print(f"Tools: {len(event.result.tool_executions)}")
asyncio.run(monitor_cost())