LLM cost estimation (#223)

* Password reset back button also after confirmation

Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>

* Implement a filter for category (#215)

- Also implement an is empty/is null condition.

* Implement an LLM cost estimation page

Track costs across all the cost categories: auto categorization, auto merchant detection and chat.
Show warning with estimated cost when running a rule that contains AI.

* Update pricing

* Add google pricing

and fix inferred model everywhere.

* Update app/models/llm_usage.rb

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: soky srm <sokysrm@gmail.com>

* FIX address review

* Linter

* Address review

- Lowered log level
- extracted the duplicated record_usage method into a shared concern

* Update app/controllers/settings/llm_usages_controller.rb

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: soky srm <sokysrm@gmail.com>

* Moved attr_reader out of private

---------

Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Signed-off-by: soky srm <sokysrm@gmail.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
soky srm
2025-10-24 00:08:59 +02:00
committed by GitHub
parent 4999409082
commit bb364fab38
19 changed files with 651 additions and 21 deletions

View File

@@ -0,0 +1,20 @@
class CreateLlmUsages < ActiveRecord::Migration[7.2]
def change
create_table :llm_usages, id: :uuid do |t|
t.references :family, null: false, foreign_key: true, type: :uuid
t.string :provider, null: false
t.string :model, null: false
t.string :operation, null: false
t.integer :prompt_tokens, null: false, default: 0
t.integer :completion_tokens, null: false, default: 0
t.integer :total_tokens, null: false, default: 0
t.decimal :estimated_cost, precision: 10, scale: 6, null: false, default: 0.0
t.jsonb :metadata, default: {}
t.timestamps
end
add_index :llm_usages, [ :family_id, :created_at ]
add_index :llm_usages, [ :family_id, :operation ]
end
end

View File

@@ -0,0 +1,6 @@
class AllowNullEstimatedCostInLlmUsages < ActiveRecord::Migration[7.2]
def change
change_column_null :llm_usages, :estimated_cost, true
change_column_default :llm_usages, :estimated_cost, from: 0.0, to: nil
end
end

20
db/schema.rb generated
View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.2].define(version: 2025_09_03_015009) do
ActiveRecord::Schema[7.2].define(version: 2025_10_22_151319) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
@@ -423,6 +423,23 @@ ActiveRecord::Schema[7.2].define(version: 2025_09_03_015009) do
t.index ["token"], name: "index_invite_codes_on_token", unique: true
end
create_table "llm_usages", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "family_id", null: false
t.string "provider", null: false
t.string "model", null: false
t.string "operation", null: false
t.integer "prompt_tokens", default: 0, null: false
t.integer "completion_tokens", default: 0, null: false
t.integer "total_tokens", default: 0, null: false
t.decimal "estimated_cost", precision: 10, scale: 6
t.jsonb "metadata", default: {}
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["family_id", "created_at"], name: "index_llm_usages_on_family_id_and_created_at"
t.index ["family_id", "operation"], name: "index_llm_usages_on_family_id_and_operation"
t.index ["family_id"], name: "index_llm_usages_on_family_id"
end
create_table "loans", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
@@ -910,6 +927,7 @@ ActiveRecord::Schema[7.2].define(version: 2025_09_03_015009) do
add_foreign_key "imports", "families"
add_foreign_key "invitations", "families"
add_foreign_key "invitations", "users", column: "inviter_id"
add_foreign_key "llm_usages", "families"
add_foreign_key "merchants", "families"
add_foreign_key "messages", "chats"
add_foreign_key "mobile_devices", "users"