Coding-agent cost tracking · Product guide
Track coding-agent costs and analyze retry loops.
Use Ganivra to measure AI cost per coding task, investigate generation and repair retries, and connect each run to customer revenue. This guide shows teams building coding agents how to record usage metadata and inspect the cost of a complete workflow.
Using Claude Code or OpenAI Codex rather than building an agent? See AI coding cost tracking by developer, team and project.
Where costs grow
How generation, compilation and retries increase coding-agent costs
Each repair can generate more output tokens. Repeatedly adding previous attempts and tool results can increase input usage too. A successful model response does not prove the build passed or the requested task was completed.
Track
Keep one task together
Use one execution ID per coding task and a unique event and step ID for each actual call. Link steps with parent IDs. Send model, token usage, latency, status, and customer context.
Use the same event ID when retrying delivery of telemetry; use a new event ID when the agent makes another paid model call.
Inspect
Find repeated expensive steps
Use Cost breakdowns to compare customers, workflows, models, and prompt versions. Open Execution explorer to inspect the sequence and cost of individual steps.
Record attempt numbers and build outcomes as bounded metadata. Rising input tokens can suggest growing context; they do not by themselves prove context saturation or a runaway loop.
Connect revenue
Understand cost-to-serve
Attach your tenant ID and configured customer plan key, or report revenue for a paid result. See whether a fixed-price coding task or subscription customer earns enough to cover tracked AI costs.
Compiler or test infrastructure cost is not discovered automatically. Supply actual tool costs when available; otherwise treat the result as AI cost, not total delivery cost.
Act
Reduce exposure to billing spikes
Use the evidence to choose retry limits, trim unnecessary context, or test a cheaper model for repair steps. Implement those controls in your agent, then compare cost alongside build success and output quality.
Ganivra surfaces the telemetry you send. It does not automatically stop loops, enforce spending caps, or guarantee savings.
Illustrative example
Coding-task cost and margin: one repair versus repeated retries
| Task | Model calls | Revenue | Tracked AI cost | Contribution |
|---|---|---|---|---|
| Completed with one repair | 2 | $1.00 | $0.20 | +$0.80 |
| Repeated repair attempts | 12 | $1.00 | $1.40 | −$0.40 |
Invented values demonstrate the calculation, not provider pricing or expected savings. The second task costs more than it earns before other business costs. Inspect the recorded steps to identify what to change.
Instrumentation example
Record coding-agent token usage and repair attempts
Send this shape to POST /v1/events with your Ganivra workspace key. Replace the illustrative IDs, counts, latency, and outcome with actual values from your application. The referenced compile step must be sent separately with the same execution ID.
{
"event_id": "evt_coding_task42_repair2",
"execution_id": "exec_coding_task42",
"step_id": "step_repair_2",
"parent_step_id": "step_compile_1",
"provider": "openai",
"model": "gpt-5",
"operation": "responses.create",
"input_tokens": 16000,
"output_tokens": 2400,
"total_tokens": 18400,
"latency_ms": 2800,
"status": "success",
"environment": "production",
"attributes": {
"application": "coding-assistant",
"customer_id": "tenant_example",
"workflow": "implement_change",
"feature": "autonomous_repair",
"prompt_id": "repair",
"prompt_version": "v2",
"attempt": 2,
"build_succeeded": false
}
}Read the example
One call can succeed while the build still fails.
status: "success"The model returned a response. Its tokens and cost belong to this step.
build_succeeded: falseYour app reports that the build failed. Keep this separate from whether the model responded.
attempt: 2Label the attempt so the recorded step has context. Each new model call gets its own event ID.
Include the compiler or test tool
Send the MCP invocation as its own step with the same execution ID. Add the server, tool, latency, and actual cost when known; link the repair call with parent_step_id.
Start with one workflow
Set up cost tracking for your coding agent
Confirm the cost of one run. Add customer pricing. Then use real evidence to choose your next improvement.