File size: 1,114 Bytes
1209812
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Basic coverage for API monitor bookkeeping and budget alerts."""

from agent.api_monitor import monitor


def test_api_monitor_tracks_calls_and_budget_alerts():
    original_limit = monitor.stats.budget_limit
    original_fraction = monitor.stats.budget_alert_fraction

    monitor.reset()
    monitor.stats.budget_limit = 0.02  # force a low budget for alerting
    monitor.stats.budget_alert_fraction = 0.5

    call_id = "test-call"
    monitor.start_call(call_id, "llm", "openai", "gpt-4o")
    monitor.complete_call(call_id, success=True, input_tokens=1000, output_tokens=2000)

    stats = monitor.get_stats()
    assert stats.total_calls == 1
    assert stats.llm_calls == 1
    assert stats.total_tokens == 3000
    assert stats.total_cost > 0
    assert stats.budget_alert_triggered is True
    assert "Budget" in stats.budget_status()

    feed = monitor.format_activity_feed()
    assert "Budget alert" in feed

    # Restore original budget settings for other tests/runs
    monitor.stats.budget_limit = original_limit
    monitor.stats.budget_alert_fraction = original_fraction
    monitor.reset()