Alright, who else is tired of the "AI line item" on the cloud bill becoming its own terrifying little creature? We're trying to be agile, not fund a model's vacation home.
I got fed up with prompt tweaks silently doubling our cost-per-session, so I built a simple CI gate. Now, if a prompt change pushes the estimated run cost over a limit, the PR fails. No more surprises.
The gist (using PromptLayer's API and GitHub Actions):
* It hooks into our PR process. Any changed `.prompt` files get parsed.
* The action calls the PromptLayer `estimate` endpoint for the new version and the main branch version.
* It compares the estimated cost per execution (using our primary model's pricing) against a threshold we set in a repo secret (`MAX_COST_PER_RUN`).
* If the new version exceeds the threshold **AND** is more expensive than the main branch version, the check fails. We get a comment on the PR with the cost diff.
Key bits of the workflow step:
```yaml
- name: Check Prompt Cost
env:
PROMPTLAYER_API_KEY: ${{ secrets.PROMPTLAYER_API_KEY }}
MAX_COST: ${{ secrets.MAX_COST_PER_RUN }}
run: |
# ...logic to fetch changed prompts...
NEW_ESTIMATE=$(curl -s -H "X-API-KEY: $PROMPTLAYER_API_KEY" "https://api.promptlayer.com/rest/estimate" ...)
# ...parse JSON for cost...
if (( $(echo "$NEW_COST > $MAX_COST" | bc -l) )); then
echo "❌ Estimated cost $$NEW_COST exceeds max of $$MAX_COST"
exit 1
fi
```
It's not perfect—it's an estimate, and it doesn't catch usage spikes—but it forces a cost conversation *before* merge. It's like a linting rule, but for your wallet.
Has anyone else tried something similar? I'm curious if there are better hooks in PromptLayer for this, or if you're just monitoring totals on the backend and screaming into the void.