Skip to content

feat: autonomous budget optimization engine (#92)#467

Open
sinatragianpaolo-oc wants to merge 1 commit intorohitdash08:mainfrom
sinatragianpaolo-oc:feat/autonomous-budget-optimization
Open

feat: autonomous budget optimization engine (#92)#467
sinatragianpaolo-oc wants to merge 1 commit intorohitdash08:mainfrom
sinatragianpaolo-oc:feat/autonomous-budget-optimization

Conversation

@sinatragianpaolo-oc
Copy link

Implements autonomous budget optimization to fix #92.

Analyses a user's spending history to detect overspending patterns, recommend category-level reallocations, and return an adaptive budget plan. Pure Python + SQLAlchemy — no external dependencies.

Endpoints

GET /budget/optimize — full analysis
GET /budget/optimize/summary — lightweight version (trend + net flow + top 3 recommendations)

Both JWT-protected. Query params: months (1–12, default 3), anchor (YYYY-MM-DD, default today).

What it detects

  • Overspending: flags categories consuming >1.5× their proportional share of total expenses
  • Trend: month-over-month average change direction (increasing / decreasing / stable)
  • Reallocations: trims each overspent category by 10%, shows freed savings opportunity
  • 50/30/20 target: when income data is present, adds a needs/wants/savings breakdown

Example response (abbreviated)

{
  "analysis_period_months": 3,
  "avg_monthly_expenses": 12400.00,
  "avg_monthly_income": 15000.00,
  "net_flow": 2600.00,
  "trend": { "direction": "increasing", "avg_mom_change_pct": 8.3 },
  "overspending_alerts": [
    {
      "category_name": "Entertainment",
      "amount": 4200.00,
      "share_pct": 33.9,
      "excess": 2050.00,
      "reason": "This category accounts for 33.9% of total expenses..."
    }
  ],
  "recommendations": [
    { "type": "trim", "category_name": "Entertainment", "savings_opportunity": 420.00, ... },
    { "type": "target_budget", "needs_target": 7500.00, "wants_target": 4500.00, "savings_target": 3000.00, ... }
  ]
}

Files

  • app/services/budget_optimizer.py — core analysis engine
  • app/routes/budget.py — Flask blueprint
  • app/routes/__init__.py — blueprint registration
  • tests/test_budget_optimizer.py — 32 tests (unit + integration)

/claim #92

Closes #92

Implements issue rohitdash08#92: detect overspending patterns, recommend category-level
reallocations, and return an adaptive budget plan based on spending history.

New files:
  packages/backend/app/services/budget_optimizer.py
    - get_budget_optimization(uid, months, anchor) → full analysis dict
    - _iter_months(): generates (year, month) tuples for the look-back window
    - _monthly_category_spend(): per-category expense aggregation per month
    - _monthly_income(): income aggregation per month
    - _detect_overspending(): flags categories consuming >1.5× their fair share
    - _compute_trend(): MoM average change + direction (increasing/decreasing/stable)
    - _build_reallocations(): trim suggestions (−10% on flagged categories) +
      50/30/20 target budget when income is available
    - No external dependencies — pure Python + SQLAlchemy

  packages/backend/app/routes/budget.py
    - GET /budget/optimize?months=3&anchor=YYYY-MM-DD  — full analysis
    - GET /budget/optimize/summary                      — lightweight summary
    - Input validation: months 1–12, anchor ISO date
    - JWT-protected, per-user isolation

  packages/backend/tests/test_budget_optimizer.py
    - 32 tests covering: unit (iter_months, detect_overspending, compute_trend),
      service (empty history, structure), HTTP (auth, params validation,
      overspend detection, income→50/30/20, net flow, user isolation, summary)

Updated:
  packages/backend/app/routes/__init__.py — register budget_bp at /budget

/claim rohitdash08#92

Closes rohitdash08#92
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autonomous Budget Optimization

1 participant