1919from gitmate .config import GitmateConfig
2020from gitmate .diff_extractor import DiffExtractor , GitCommandError
2121from gitmate .fallback import generate_commit_message
22- from gitmate .metrics import record_invocation
22+ from gitmate .metrics import get_monthly_spend , record_invocation
2323from gitmate .providers .base import LLMProvider
2424from gitmate .token_budget import TokenCounter
2525
@@ -39,6 +39,7 @@ def _safe_record_telemetry(
3939 cache_hit : bool ,
4040 latency_ms : int | None ,
4141 fallback_used : bool ,
42+ estimated_cost_usd : float | None = None ,
4243) -> None :
4344 """Record invocation to metrics database without propagating errors."""
4445 try :
@@ -50,6 +51,7 @@ def _safe_record_telemetry(
5051 cache_hit = cache_hit ,
5152 latency_ms = latency_ms ,
5253 fallback_used = fallback_used ,
54+ estimated_cost_usd = estimated_cost_usd ,
5355 )
5456 except Exception as exc : # noqa: BLE001
5557 logger .warning ("Failed to record telemetry: %s" , exc )
@@ -232,6 +234,23 @@ def commit_flow(
232234 )
233235 return 1
234236
237+ # 3. Monthly budget cap enforcement
238+ if active_cfg .budget_cap_usd is not None :
239+ monthly_spend = get_monthly_spend ()
240+ if monthly_spend >= active_cfg .budget_cap_usd :
241+ con .print (
242+ f"[red]error:[/red] Monthly budget cap exceeded "
243+ f"(${ monthly_spend :.2f} >= ${ active_cfg .budget_cap_usd :.2f} ). "
244+ "Aborting to prevent further LLM spend.\n "
245+ "To adjust or remove the cap, run: gitmate config set budget_cap_usd <new_limit_or_none>"
246+ )
247+ return 1
248+ if monthly_spend >= 0.8 * active_cfg .budget_cap_usd :
249+ con .print (
250+ f"[yellow]warning: Monthly spend has reached { int (monthly_spend / active_cfg .budget_cap_usd * 100 )} % "
251+ f"of budget cap (${ monthly_spend :.2f} / ${ active_cfg .budget_cap_usd :.2f} ).[/yellow]"
252+ )
253+
235254 active_counter = counter
236255 if active_counter is None and provider is not None :
237256
@@ -241,7 +260,7 @@ def count_tokens(self, text: str, model: str) -> int:
241260
242261 active_counter = _SimpleCounter ()
243262
244- # 3 . Initial generation & telemetry recording
263+ # 4 . Initial generation & telemetry recording
245264 t0 = time .perf_counter ()
246265 gen_result = generate_commit_message (
247266 diffs = staged_diffs ,
@@ -262,18 +281,19 @@ def count_tokens(self, text: str, model: str) -> int:
262281 cache_hit = gen_result .cache_hit ,
263282 latency_ms = latency_ms ,
264283 fallback_used = gen_result .is_fallback ,
284+ estimated_cost_usd = gen_result .estimated_cost_usd ,
265285 )
266286
267287 current_message = gen_result .text
268288 current_model = gen_result .model
269289 current_is_fallback = gen_result .is_fallback
270290
271- # 4 . Fast path: --yes with allow_noninteractive_commit enabled
291+ # 5 . Fast path: --yes with allow_noninteractive_commit enabled
272292 if yes :
273293 code = _execute_commit (current_message , console = con , cwd = repo_dir )
274294 return code
275295
276- # 5 . Interactive review loop
296+ # 6 . Interactive review loop
277297 _render_panel (con , current_message , current_model , current_is_fallback )
278298
279299 while True :
@@ -305,6 +325,22 @@ def count_tokens(self, text: str, model: str) -> int:
305325 _render_panel (con , current_message , current_model , current_is_fallback )
306326
307327 elif choice in ("r" , "regenerate" ):
328+ if active_cfg .budget_cap_usd is not None :
329+ monthly_spend = get_monthly_spend ()
330+ if monthly_spend >= active_cfg .budget_cap_usd :
331+ con .print (
332+ f"[red]error:[/red] Monthly budget cap exceeded "
333+ f"(${ monthly_spend :.2f} >= ${ active_cfg .budget_cap_usd :.2f} ). "
334+ "Aborting to prevent further LLM spend.\n "
335+ "To adjust or remove the cap, run: gitmate config set budget_cap_usd <new_limit_or_none>"
336+ )
337+ continue
338+ if monthly_spend >= 0.8 * active_cfg .budget_cap_usd :
339+ con .print (
340+ f"[yellow]warning: Monthly spend has reached { int (monthly_spend / active_cfg .budget_cap_usd * 100 )} % "
341+ f"of budget cap (${ monthly_spend :.2f} / ${ active_cfg .budget_cap_usd :.2f} ).[/yellow]"
342+ )
343+
308344 con .print ("Regenerating commit message with fresh API call..." )
309345 t0 = time .perf_counter ()
310346 gen_result = generate_commit_message (
@@ -326,6 +362,7 @@ def count_tokens(self, text: str, model: str) -> int:
326362 cache_hit = False ,
327363 latency_ms = latency_ms ,
328364 fallback_used = gen_result .is_fallback ,
365+ estimated_cost_usd = gen_result .estimated_cost_usd ,
329366 )
330367
331368 current_message = gen_result .text
0 commit comments