Skip to content

Commit 2d1ffba

Browse files
committed
Add cost tracking and centralized logging; propagate LLM operation metadata
- Introduce cost tracking system and CLI: - Add brain_core/cost_tracker.py for recording/aggregating LLM usage - Add brain_cli/cost_commands.py to view summary, trends, estimate, breakdown, export, pricing - Add cost.json example data - Update .env.example and README with cost/logging/pricing options and new commands - Centralized logging: - Add brain_core/logging_config.py with setup_logging, helpers, and LLM/timing log functions - Wire setup_logging into brain_cli/main.py and add --verbose/--debug flags + version callback - LLM usage metadata and cost recording: - Extend LLMClient.generate_sync signature to accept operation and entry_date - AzureOpenAIClient now records token usage, estimates cost via CostTracker, and calls log_llm_call - Propagate operation/entry_date parameters in template_generator, llm_analysis, report_generator, plan_commands - Behavior and config tweaks: - Update brain_core/config.py to expose cost/logging config - Update constants: DAILY_PROMPT_COUNT=2, PROMPT_CONTEXT_DAYS=2, add AZURE_OPENAI_PRICING map - Lower-level logging swapped to debug for LLM/prompt generation messages - Tests: - Update tests to use create_autospec(LLMClient) and assert operation/entry_date are passed for task extraction This brings end-to-end cost tracking, richer logging, and consistent propagation of LLM call metadata across the codebase.
1 parent 6aff6fb commit 2d1ffba

File tree

16 files changed

+1276
-46
lines changed

16 files changed

+1276
-46
lines changed

.env.example

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,20 @@ AZURE_SEARCH_ENDPOINT=https://your-search-service.search.windows.net
2020
AZURE_SEARCH_API_KEY=your-search-api-key-here
2121
AZURE_SEARCH_INDEX_NAME=second-brain-notes
2222

23-
# Optional: Daemon configuration
24-
DAEMON_AUTO_LINK_TIME=23:00
25-
DAEMON_WEEKLY_ANALYSIS=true
26-
DAEMON_REFRESH_DAYS=30
23+
# Optional: Cost Tracking Configuration
24+
BRAIN_COST_DB_PATH=~/.brain/costs.db
25+
26+
# Optional: Logging Configuration
27+
BRAIN_LOG_LEVEL=INFO
28+
BRAIN_LOG_FILE=~/.brain/logs/brain.log
29+
30+
# Optional: Custom Pricing (override Azure OpenAI pricing)
31+
# Prices are per 1K tokens
32+
AZURE_GPT4O_INPUT_PRICE=0.03
33+
AZURE_GPT4O_OUTPUT_PRICE=0.06
34+
AZURE_GPT4O_MINI_INPUT_PRICE=0.0015
35+
AZURE_GPT4O_MINI_OUTPUT_PRICE=0.006
36+
AZURE_GPT4_INPUT_PRICE=0.03
37+
AZURE_GPT4_OUTPUT_PRICE=0.06
38+
AZURE_GPT35_TURBO_INPUT_PRICE=0.0015
39+
AZURE_GPT35_TURBO_OUTPUT_PRICE=0.002

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ AI-powered journaling with semantic backlinks and intelligent book notes search.
2323
- Rich formatted results
2424
- **Requires Azure AI Search** (no local alternative)
2525

26+
**Cost Tracking:**
27+
- Real-time Azure OpenAI usage tracking
28+
- Local SQLite database storage
29+
- Cost summaries, trends, and projections
30+
- Per-operation breakdowns (diary, planning, analysis)
31+
- Export to CSV for billing analysis
32+
33+
**Logging:**
34+
- Rich-formatted colored console output
35+
- Clean UX with smart log suppression (no interruptions during spinners)
36+
- Multiple verbosity levels (--verbose, --debug)
37+
- File and console output options
38+
- Suppressed noisy third-party HTTP logs
39+
2640
**LLM Support:**
2741
- Azure OpenAI - Full functionality for all commands
2842

@@ -68,9 +82,23 @@ uv run brain notes search "topic" # Search book notes
6882
uv run brain notes search "topic" --semantic --detailed
6983
uv run brain notes status # Check connection
7084

71-
85+
# Cost tracking and analysis
86+
uv run brain cost summary # Usage summary for last 30 days
87+
uv run brain cost trends 30 # Daily cost trends
88+
uv run brain cost estimate # Monthly cost projection
89+
uv run brain cost breakdown # Per-operation breakdown
90+
uv run brain cost export data.json # Export to JSON
91+
uv run brain cost pricing # Show pricing
92+
93+
# Logging (Rich-formatted, colored output)
94+
uv run brain --verbose <command> # Show key operations
95+
uv run brain --debug <command> # Full diagnostics with LLM details
7296
```
7397

98+
> **Clean UX:** By default, logs are suppressed during operations to avoid interrupting spinners. Use `--verbose` or `--debug` for detailed information with beautiful Rich formatting.
99+
100+
> **Cost Tracking:** All Azure OpenAI usage is automatically tracked in a local SQLite database (`~/.brain/costs.db`). Data stays private and grows ~10-50 MB/year for typical use.
101+
74102
## Configuration
75103

76104
**Required Paths:**
@@ -88,6 +116,21 @@ uv run brain notes status # Check connection
88116
- `AZURE_SEARCH_API_KEY` - Azure AI Search API key
89117
- `AZURE_SEARCH_INDEX_NAME` - Search index name (default: second-brain-notes)
90118

119+
**Cost Tracking** (optional configuration):
120+
- `BRAIN_COST_DB_PATH` - Path to cost tracking database (default: ~/.brain/costs.db)
121+
- `BRAIN_LOG_LEVEL` - Logging level: DEBUG, INFO, WARNING, ERROR (default: INFO)
122+
- `BRAIN_LOG_FILE` - Path to log file (optional, logs to console if not set)
123+
124+
**Custom Pricing** (optional - override Azure OpenAI pricing):
125+
- `AZURE_GPT4O_INPUT_PRICE` - Price per 1K input tokens for gpt-4o (default: 0.03)
126+
- `AZURE_GPT4O_OUTPUT_PRICE` - Price per 1K output tokens for gpt-4o (default: 0.06)
127+
- `AZURE_GPT4O_MINI_INPUT_PRICE` - Price per 1K input tokens for gpt-4o-mini (default: 0.0015)
128+
- `AZURE_GPT4O_MINI_OUTPUT_PRICE` - Price per 1K output tokens for gpt-4o-mini (default: 0.006)
129+
- `AZURE_GPT4_INPUT_PRICE` - Price per 1K input tokens for gpt-4 (default: 0.03)
130+
- `AZURE_GPT4_OUTPUT_PRICE` - Price per 1K output tokens for gpt-4 (default: 0.06)
131+
- `AZURE_GPT35_TURBO_INPUT_PRICE` - Price per 1K input tokens for gpt-35-turbo (default: 0.0015)
132+
- `AZURE_GPT35_TURBO_OUTPUT_PRICE` - Price per 1K output tokens for gpt-35-turbo (default: 0.002)
133+
91134
## Entry Format
92135

93136
**Morning Plan Entry** (`YYYY-MM-DD-plan.md`):
@@ -147,6 +190,8 @@ uv run pytest --cov # Run with coverage
147190
uv run pytest tests/brain_core/ # Run specific module
148191
```
149192

193+
**Coverage**: 74 tests with 45% overall coverage, 96% on critical paths (plan_commands.py)
194+
150195
## License
151196

152197
MIT

0 commit comments

Comments
 (0)