Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/goose-server/src/routes/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub async fn get_token_state(session_manager: &SessionManager, session_id: &str)
accumulated_input_tokens: session.accumulated_input_tokens.unwrap_or(0),
accumulated_output_tokens: session.accumulated_output_tokens.unwrap_or(0),
accumulated_total_tokens: session.accumulated_total_tokens.unwrap_or(0),
accumulated_cost: session.accumulated_cost,
})
.inspect_err(|e| {
tracing::warn!(
Expand Down
1 change: 1 addition & 0 deletions crates/goose/src/acp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4293,6 +4293,7 @@ print(\"hello, world\")
accumulated_total_tokens,
accumulated_input_tokens,
accumulated_output_tokens,
accumulated_cost: None,
schedule_id: None,
recipe: None,
user_recipe_values: None,
Expand Down
27 changes: 27 additions & 0 deletions crates/goose/src/agents/reply_parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@ impl Agent {
let accumulated_output =
accumulate(session.accumulated_output_tokens, usage.usage.output_tokens);

let accumulated_cost = session
.provider_name
.as_deref()
.and_then(|pn| self.accumulate_cost(session.accumulated_cost, usage, pn))
.or(session.accumulated_cost);

let (current_total, current_input, current_output) = if is_compaction_usage {
// After compaction: summary output becomes new input context
let new_input = usage.usage.output_tokens;
Expand All @@ -497,11 +503,32 @@ impl Agent {
.accumulated_total_tokens(accumulated_total)
.accumulated_input_tokens(accumulated_input)
.accumulated_output_tokens(accumulated_output)
.accumulated_cost(accumulated_cost)
.apply()
.await?;

Ok(())
}

fn accumulate_cost(
&self,
existing: Option<f64>,
usage: &ProviderUsage,
provider_name: &str,
) -> Option<f64> {
let canonical =
crate::providers::canonical::maybe_get_canonical_model(provider_name, &usage.model)?;

let input_price = canonical.cost.input?;
let output_price = canonical.cost.output?;

let input_tokens = usage.usage.input_tokens.unwrap_or(0) as f64;
let output_tokens = usage.usage.output_tokens.unwrap_or(0) as f64;

let chunk_cost = (input_tokens * input_price + output_tokens * output_price) / 1_000_000.0;

Some(existing.unwrap_or(0.0) + chunk_cost)
}
}

/// Check whether a tool should be callable by an app based on MCP Apps visibility metadata.
Expand Down
1 change: 1 addition & 0 deletions crates/goose/src/conversation/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,7 @@ pub struct TokenState {
pub accumulated_input_tokens: i32,
pub accumulated_output_tokens: i32,
pub accumulated_total_tokens: i32,
pub accumulated_cost: Option<f64>,
}

#[cfg(test)]
Expand Down
37 changes: 35 additions & 2 deletions crates/goose/src/session/session_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::sync::{Arc, LazyLock};
use tracing::{info, warn};
use utoipa::ToSchema;

pub const CURRENT_SCHEMA_VERSION: i32 = 12;
pub const CURRENT_SCHEMA_VERSION: i32 = 13;
pub const SESSIONS_FOLDER: &str = "sessions";
pub const DB_NAME: &str = "sessions.db";

Expand Down Expand Up @@ -72,6 +72,7 @@ pub struct Session {
pub accumulated_total_tokens: Option<i32>,
pub accumulated_input_tokens: Option<i32>,
pub accumulated_output_tokens: Option<i32>,
pub accumulated_cost: Option<f64>,
pub schedule_id: Option<String>,
pub recipe: Option<Recipe>,
pub user_recipe_values: Option<HashMap<String, String>>,
Expand Down Expand Up @@ -101,6 +102,7 @@ pub struct SessionUpdateBuilder<'a> {
accumulated_total_tokens: Option<Option<i32>>,
accumulated_input_tokens: Option<Option<i32>>,
accumulated_output_tokens: Option<Option<i32>>,
accumulated_cost: Option<Option<f64>>,
schedule_id: Option<Option<String>>,
recipe: Option<Option<Recipe>>,
user_recipe_values: Option<Option<HashMap<String, String>>>,
Expand Down Expand Up @@ -135,6 +137,7 @@ impl<'a> SessionUpdateBuilder<'a> {
accumulated_total_tokens: None,
accumulated_input_tokens: None,
accumulated_output_tokens: None,
accumulated_cost: None,
schedule_id: None,
recipe: None,
user_recipe_values: None,
Expand Down Expand Up @@ -213,6 +216,11 @@ impl<'a> SessionUpdateBuilder<'a> {
self
}

pub fn accumulated_cost(mut self, cost: Option<f64>) -> Self {
self.accumulated_cost = Some(cost);
self
}

pub fn schedule_id(mut self, schedule_id: Option<String>) -> Self {
self.schedule_id = Some(schedule_id);
self
Expand Down Expand Up @@ -490,6 +498,7 @@ impl Default for Session {
accumulated_total_tokens: None,
accumulated_input_tokens: None,
accumulated_output_tokens: None,
accumulated_cost: None,
schedule_id: None,
recipe: None,
user_recipe_values: None,
Expand Down Expand Up @@ -557,6 +566,7 @@ impl sqlx::FromRow<'_, sqlx::sqlite::SqliteRow> for Session {
accumulated_total_tokens: row.try_get("accumulated_total_tokens")?,
accumulated_input_tokens: row.try_get("accumulated_input_tokens")?,
accumulated_output_tokens: row.try_get("accumulated_output_tokens")?,
accumulated_cost: row.try_get("accumulated_cost").ok().flatten(),
schedule_id: row.try_get("schedule_id")?,
recipe,
user_recipe_values,
Expand Down Expand Up @@ -666,6 +676,7 @@ impl SessionStorage {
accumulated_total_tokens INTEGER,
accumulated_input_tokens INTEGER,
accumulated_output_tokens INTEGER,
accumulated_cost REAL,
schedule_id TEXT,
recipe_json TEXT,
user_recipe_values_json TEXT,
Expand Down Expand Up @@ -786,9 +797,10 @@ impl SessionStorage {
id, name, user_set_name, session_type, working_dir, created_at, updated_at, extension_data,
total_tokens, input_tokens, output_tokens,
accumulated_total_tokens, accumulated_input_tokens, accumulated_output_tokens,
accumulated_cost,
schedule_id, recipe_json, user_recipe_values_json,
provider_name, model_config_json, goose_mode
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
"#,
)
.bind(&session.id)
Expand All @@ -805,6 +817,7 @@ impl SessionStorage {
.bind(session.accumulated_total_tokens)
.bind(session.accumulated_input_tokens)
.bind(session.accumulated_output_tokens)
.bind(session.accumulated_cost)
.bind(&session.schedule_id)
.bind(recipe_json)
.bind(user_recipe_values_json)
Expand Down Expand Up @@ -1087,6 +1100,19 @@ impl SessionStorage {
.await?;
}
}
13 => {
let has_accumulated_cost = sqlx::query_scalar::<_, i32>(
"SELECT COUNT(*) FROM pragma_table_info('sessions') WHERE name = 'accumulated_cost'",
)
.fetch_one(&mut **tx)
.await?
> 0;
if !has_accumulated_cost {
sqlx::query("ALTER TABLE sessions ADD COLUMN accumulated_cost REAL")
.execute(&mut **tx)
.await?;
}
}
_ => {
anyhow::bail!("Unknown migration version: {}", version);
}
Expand Down Expand Up @@ -1147,6 +1173,7 @@ impl SessionStorage {
SELECT id, working_dir, name, description, user_set_name, session_type, created_at, updated_at, extension_data,
total_tokens, input_tokens, output_tokens,
accumulated_total_tokens, accumulated_input_tokens, accumulated_output_tokens,
accumulated_cost,
schedule_id, recipe_json, user_recipe_values_json,
provider_name, model_config_json, goose_mode,
archived_at, project_id
Expand Down Expand Up @@ -1207,6 +1234,7 @@ impl SessionStorage {
builder.accumulated_output_tokens,
"accumulated_output_tokens"
);
add_update!(builder.accumulated_cost, "accumulated_cost");
add_update!(builder.schedule_id, "schedule_id");
add_update!(builder.recipe, "recipe_json");
add_update!(builder.user_recipe_values, "user_recipe_values_json");
Expand Down Expand Up @@ -1259,6 +1287,9 @@ impl SessionStorage {
if let Some(aot) = builder.accumulated_output_tokens {
q = q.bind(aot);
}
if let Some(ac) = builder.accumulated_cost {
q = q.bind(ac);
}
if let Some(sid) = builder.schedule_id {
q = q.bind(sid);
}
Expand Down Expand Up @@ -1445,6 +1476,7 @@ impl SessionStorage {
SELECT s.id, s.working_dir, s.name, s.description, s.user_set_name, s.session_type, s.created_at, s.updated_at, s.extension_data,
s.total_tokens, s.input_tokens, s.output_tokens,
s.accumulated_total_tokens, s.accumulated_input_tokens, s.accumulated_output_tokens,
s.accumulated_cost,
s.schedule_id, s.recipe_json, s.user_recipe_values_json,
s.provider_name, s.model_config_json, s.goose_mode,
s.archived_at, s.project_id,
Expand Down Expand Up @@ -1564,6 +1596,7 @@ impl SessionStorage {
.accumulated_total_tokens(import.accumulated_total_tokens)
.accumulated_input_tokens(import.accumulated_input_tokens)
.accumulated_output_tokens(import.accumulated_output_tokens)
.accumulated_cost(import.accumulated_cost)
.schedule_id(import.schedule_id)
.recipe(import.recipe)
.user_recipe_values(import.user_recipe_values);
Expand Down
10 changes: 10 additions & 0 deletions ui/desktop/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -7868,6 +7868,11 @@
"message_count"
],
"properties": {
"accumulated_cost": {
"type": "number",
"format": "double",
"nullable": true
},
"accumulated_input_tokens": {
"type": "integer",
"format": "int32",
Expand Down Expand Up @@ -8567,6 +8572,11 @@
"accumulatedTotalTokens"
],
"properties": {
"accumulatedCost": {
"type": "number",
"format": "double",
"nullable": true
},
"accumulatedInputTokens": {
"type": "integer",
"format": "int32"
Expand Down
2 changes: 2 additions & 0 deletions ui/desktop/src/api/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,7 @@ export type ScheduledJob = {
};

export type Session = {
accumulated_cost?: number | null;
accumulated_input_tokens?: number | null;
accumulated_output_tokens?: number | null;
accumulated_total_tokens?: number | null;
Expand Down Expand Up @@ -1480,6 +1481,7 @@ export type ThinkingContent = {
};

export type TokenState = {
accumulatedCost?: number | null;
accumulatedInputTokens: number;
accumulatedOutputTokens: number;
accumulatedTotalTokens: number;
Expand Down
14 changes: 4 additions & 10 deletions ui/desktop/src/components/BaseChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { RecipeHeader } from './RecipeHeader';
import { RecipeWarningModal } from './ui/RecipeWarningModal';
import { scanRecipe } from '../recipe';
import { UserInput } from '../types/message';
import { useCostTracking } from '../hooks/useCostTracking';
import RecipeActivities from './recipes/RecipeActivities';
import { useToolCount } from './alerts/useToolCount';
import { getThinkingMessage, getTextAndImageContent } from '../types/message';
Expand Down Expand Up @@ -196,14 +195,6 @@ export default function BaseChat({
handleSubmit(input);
};

const { sessionCosts } = useCostTracking({
sessionInputTokens: session?.accumulated_input_tokens || 0,
sessionOutputTokens: session?.accumulated_output_tokens || 0,
localInputTokens: 0,
localOutputTokens: 0,
session,
});

const sessionModel = session?.model_config?.model_name ?? null;
const sessionProvider = session?.provider_name ?? null;
const sessionLoaded = session !== undefined;
Expand Down Expand Up @@ -511,11 +502,14 @@ export default function BaseChat({
accumulatedOutputTokens={
tokenState?.accumulatedOutputTokens ?? session?.accumulated_output_tokens ?? undefined
}
accumulatedCost={
tokenState?.accumulatedCost ?? session?.accumulated_cost ?? undefined
}
droppedFiles={droppedFiles}
onFilesProcessed={() => setDroppedFiles([])} // Clear dropped files after processing
messages={messages}
disableAnimation={disableAnimation}
sessionCosts={sessionCosts}

recipe={recipe}
recipeAccepted={!hasNotAcceptedRecipe}
initialPrompt={initialPrompt}
Expand Down
12 changes: 3 additions & 9 deletions ui/desktop/src/components/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,8 @@ interface ChatInputProps {
totalTokens?: number;
accumulatedInputTokens?: number;
accumulatedOutputTokens?: number;
accumulatedCost?: number | null;
messages?: Message[];
sessionCosts?: {
[key: string]: {
inputTokens: number;
outputTokens: number;
totalCost: number;
};
};
disableAnimation?: boolean;
recipe?: Recipe | null;
recipeId?: string | null;
Expand Down Expand Up @@ -203,9 +197,9 @@ export default function ChatInput({
totalTokens,
accumulatedInputTokens,
accumulatedOutputTokens,
accumulatedCost,
messages = [],
disableAnimation = false,
sessionCosts,
recipe,
recipeId,
recipeAccepted,
Expand Down Expand Up @@ -1690,7 +1684,7 @@ export default function ChatInput({
<CostTracker
inputTokens={accumulatedInputTokens}
outputTokens={accumulatedOutputTokens}
sessionCosts={sessionCosts}
accumulatedCost={accumulatedCost}
model={effectiveModel}
provider={effectiveProvider}
/>
Expand Down
1 change: 0 additions & 1 deletion ui/desktop/src/components/Hub.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ export default function Hub({
onFilesProcessed={() => {}}
messages={[]}
disableAnimation={false}
sessionCosts={undefined}
toolCount={0}
onWorkingDirChange={setWorkingDir}
inputRef={inputRef}
Expand Down
Loading
Loading