Skip to content

Commit ab96375

Browse files
tbitcsoz-agent
andcommitted
feat: auto-init credit tracking, agent credit instructions, session-end credits
Credit tracking is now automatic: - specsmith init creates .specsmith/credit-budget.json (unlimited default) - specsmith import creates .specsmith/ on first import - .specsmith/ added to .gitignore template (credit data is local-only) - .specsmith/ excluded from importer file scan Agent integrations include credit instructions: - Warp SKILL.md: credit recording command + budget check - Claude CLAUDE.md: credit recording with anthropic provider - context-budget.md.j2 template: credit tracking section for all projects Session-end checklist includes credit summary: - Shows total spend and session count - Surfaces budget alerts (watermark, cap warnings) Co-Authored-By: Oz <oz-agent@warp.dev>
1 parent 9c35c00 commit ab96375

7 files changed

Lines changed: 67 additions & 0 deletions

File tree

src/specsmith/importer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ def detect_project(root: Path) -> DetectionResult:
168168
".ruff_cache",
169169
".mypy_cache",
170170
".work",
171+
".specsmith",
171172
"build",
172173
"dist",
173174
"target",
@@ -1117,6 +1118,14 @@ def _write(rel_path: str, content: str) -> None:
11171118
agents_path.write_text(hub, encoding="utf-8")
11181119
created.append(agents_path)
11191120

1121+
# Initialize credit tracking with unlimited budget
1122+
specsmith_dir = target / ".specsmith"
1123+
if not specsmith_dir.exists():
1124+
from specsmith.credits import CreditBudget, save_budget
1125+
1126+
save_budget(target, CreditBudget()) # unlimited by default
1127+
created.append(target / ".specsmith" / "credit-budget.json")
1128+
11201129
# --- CI config (merge: only create if no CI detected) ---
11211130
if not result.existing_ci and result.vcs_platform:
11221131
try:

src/specsmith/integrations/claude_code.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,10 @@ def _render(self, config: ProjectConfig) -> str:
7878
When user says `pr`: run `specsmith pr --project-dir .`
7979
When user says `audit`: run `specsmith audit --project-dir .`
8080
When user says `session-end`: run `specsmith session-end --project-dir .`
81+
82+
## Credit Tracking
83+
At session end, record token usage:
84+
`specsmith credits record --model <model> --provider anthropic \
85+
--tokens-in <N> --tokens-out <N> --task "<desc>"`
86+
Check budget: `specsmith credits summary`
8187
"""

src/specsmith/integrations/warp.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,19 @@ def _render_skill(self, config: ProjectConfig) -> str:
8484
## Verification
8585
Before marking any task complete, run: {verify_line}
8686
87+
## Credit Tracking
88+
After completing tasks, record token usage:
89+
```
90+
specsmith credits record --model <model> --provider <provider> \
91+
--tokens-in <N> --tokens-out <N> --task "<desc>"
92+
```
93+
Check budget: `specsmith credits summary`
94+
8795
## Rules
8896
- Proposals before changes (no exceptions)
8997
- Verify before recording completion
9098
- Use execution shims (`scripts/exec.cmd` / `scripts/exec.sh`) for external commands
9199
- Keep AGENTS.md under 200 lines
92100
- Record every session in the ledger
101+
- Record credit usage at session end
93102
"""

src/specsmith/scaffolder.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ def scaffold_project(config: ProjectConfig, target: Path) -> list[Path]:
8080
except ValueError:
8181
pass # Unknown platform — skip silently
8282

83+
# Initialize credit tracking with unlimited budget
84+
from specsmith.credits import CreditBudget, save_budget
85+
86+
save_budget(target, CreditBudget()) # unlimited by default
87+
created.append(target / ".specsmith" / "credit-budget.json")
88+
8389
# Git init
8490
if config.git_init:
8591
subprocess.run( # noqa: S603

src/specsmith/session.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,27 @@ def run_session_end(root: Path) -> SessionReport:
127127
SessionCheck(name="audit", status="warn", message="Could not run audit")
128128
)
129129

130+
# Credit spend summary for this session
131+
try:
132+
from specsmith.credits import get_summary
133+
134+
cs = get_summary(root)
135+
if cs.entry_count > 0:
136+
report.checks.append(
137+
SessionCheck(
138+
name="credits",
139+
status="ok",
140+
message=(
141+
f"Credits: ${cs.total_cost_usd:.4f} total, "
142+
f"{cs.session_count} session(s)"
143+
),
144+
)
145+
)
146+
for alert in cs.alerts:
147+
report.checks.append(
148+
SessionCheck(name="credit-alert", status="warn", message=alert)
149+
)
150+
except Exception: # noqa: BLE001
151+
pass # Credits not configured — skip silently
152+
130153
return report

src/specsmith/templates/gitignore.j2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ htmlcov/
3636
# Build artifacts
3737
.work/
3838
*.log
39+
40+
# specsmith local data (credits, config)
41+
.specsmith/
3942
{% if project.type.value in ('cli-rust', 'library-rust') %}
4043

4144
# Rust

src/specsmith/templates/governance/context-budget.md.j2

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,14 @@ If a cheaper check fails, fix that before running more expensive checks.
4848
- **low** — docs-only, single-file edits, small scaffolds
4949
- **medium** — multi-file implementation, routine refactors, standard test runs
5050
- **high** — architecture changes, large builds, broad audits
51+
52+
## Credit tracking
53+
54+
This project tracks AI credit spend automatically. At the end of each session:
55+
56+
1. Record usage: `specsmith credits record --model <model> --provider <provider> --tokens-in <N> --tokens-out <N> --task "<description>"`
57+
2. Check budget: `specsmith credits summary`
58+
3. If budget alerts appear, review with: `specsmith credits analyze`
59+
60+
Budget configuration: `specsmith credits budget --cap <USD> --alert-pct 80`
61+
Credit data stored in `.specsmith/credits.json` (gitignored).

0 commit comments

Comments
 (0)