---
title: "Controlling costs when building with AI"
description: "After hitting OpenAI billing limits broke its early AI features, incident.io built per-prompt cost attribution into its Go LLM abstraction, using reflection to capture each prompt's type name alongside token usage and emitting it to Grafana metrics and Postgres records for BI."
url: "https://swe.quest/articles/controlling-costs-when-building-with-ai-e6fb1a0a01ec2e9a"
canonical: "https://incident.io/blog/controlling-costs"
source: "incident.io"
published: "2025-03-25"
difficulty: "medium"
kind: "Case study"
reading_minutes: 10
tags: ["llms","cost-optimization","observability","go"]
---

# Controlling costs when building with AI

A post on [incident.io](https://swe.quest/sources/incident-io.md), published 2025-03-25, 10 min read, Case study. Original: [https://incident.io/blog/controlling-costs](https://incident.io/blog/controlling-costs)

## Summary

After hitting OpenAI billing limits broke its early AI features, incident.io built per-prompt cost attribution into its Go LLM abstraction, using reflection to capture each prompt's type name alongside token usage and emitting it to Grafana metrics and Postgres records for BI. Separate OpenAI projects per feature, mapped from source file patterns and provisioned with Terraform, give each surface its own billing limit to contain runaway spend. Daily Slack spend reports, per-message cost and latency in dev, backfill cost predictions from historical runs, and eval suites that report their cost keep spend visible to developers and let teams model feature costs before launch.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Cost optimization](https://swe.quest/tags/cost-optimization.md) (Reliability & operations)
- [Observability](https://swe.quest/tags/observability.md) (Reliability & operations)
- [Go](https://swe.quest/tags/go.md) (Languages)

## Prerequisites

- LLM API token pricing ([easy posts on LLMs](https://swe.quest/tags/llms.md?difficulty=easy))
- Go generics and reflection ([easy posts on Go](https://swe.quest/tags/go.md?difficulty=easy))

## Keywords

OpenAI, incident.io, Grafana, Terraform, Postgres, Slack

## Related posts

- [Everything We Talked About at O11yDay London 2026](https://swe.quest/articles/everything-we-talked-about-at-o11yday-london-2026-0f7b0e96a8f38f57.md): Honeycomb · 2026-10-16 · Medium · 11 min read · AI agents, Observability, AI for coding, Code review, Cost optimization
  Across ten sessions at O11yDay London, the recurring bottleneck was not writing code but reviewing it, operating agents in production, and proving the spend is worth it.
- [LLM Cost Optimization: 3 Patterns to Cut LLM Token Spend](https://swe.quest/articles/llm-cost-optimization-3-patterns-to-cut-llm-token-spend-f19f5dd3713227bf.md): Honeycomb · 2026-11-05 · Medium · 9 min read · LLMs, Cost optimization, Observability, AI agents
  LLM token spend is best reduced by using traces to find three waste patterns: repeated input context that doesn't improve results, retry storms and runaway agent loops, and output tokens that don't lift outcomes.
- [Optimizing LLM prompts for low latency](https://swe.quest/articles/optimizing-llm-prompts-for-low-latency-a98e3093827ae50c.md): incident.io · 2025-03-25 · Medium · 9 min read · LLMs, Performance engineering, Inference & serving
  incident.io cut latency of an LLM prompt that plans Grafana dashboards for incident investigations from 11s to under 2.3s without changing its behaviour, mainly by shrinking output tokens.
- [Comparing token economics across 42 AI models](https://swe.quest/articles/comparing-token-economics-across-42-ai-models-653e0d85d2add5a0.md): Neon · 2026-09-04 · Medium · 10 min read · Evaluation, LLMs, Cost optimization, Inference & serving
  A synthetic workload of 100 support tickets (20 scenarios × 5 tone variants) was run through 42 models via Neon AI Gateway, with responses graded on seven checks including JSON validity, correct classification and escalation, and policy compliance.
- [Introducing Spend Management: Spending limits and alerts for Cloudflare](https://swe.quest/articles/introducing-spend-management-spending-limits-and-alerts-for-cloudflare-dcd9025e1197ec48.md): Cloudflare · 2026-11-13 · Medium · 9 min read · Cost optimization, System design, Serverless, Data pipelines
  Cloudflare Spend Management, now in beta for pay-as-you-go customers, lets users set spending limits and alert thresholds on Workers and Durable Objects, automatically pausing billable invocations when a cap is hit.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
