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
12 changes: 1 addition & 11 deletions crates/goose-cli/src/commands/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,7 @@ impl BenchAgent for BenchSession {
}

async fn get_token_usage(&self) -> Option<i32> {
// Get token usage from the provider
if let Ok(usage) = self.session.get_usage().await {
// Sum up total tokens across all usage entries
let total_tokens = usage
.iter()
.map(|u| u.usage.total_tokens.unwrap_or(0))
.sum();
Some(total_tokens)
} else {
None
}
self.session.get_total_token_usage().ok().flatten()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zakiali @laanak08 wanted to double check this looks good for goose bench

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, this looks good!

}
}

Expand Down
1 change: 0 additions & 1 deletion crates/goose-cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use etcetera::AppStrategyArgs;
use once_cell::sync::Lazy;
pub mod commands;
pub mod log_usage;
pub mod logging;
pub mod session;

Expand Down
107 changes: 0 additions & 107 deletions crates/goose-cli/src/log_usage.rs

This file was deleted.

36 changes: 17 additions & 19 deletions crates/goose-cli/src/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use goose::agents::extension::{Envs, ExtensionConfig};
use goose::agents::{Agent, SessionConfig};
use goose::config::Config;
use goose::message::{Message, MessageContent};
use goose::providers::base::ProviderUsage;
use goose::session;
use mcp_core::handler::ToolError;
use mcp_core::prompt::PromptMessage;
Expand All @@ -29,8 +28,6 @@ use std::sync::Arc;
use std::time::Instant;
use tokio;

use crate::log_usage::log_usage;

pub struct Session {
agent: Box<dyn Agent>,
messages: Vec<Message>,
Expand Down Expand Up @@ -413,19 +410,10 @@ impl Session {
}
}

// Log usage and cleanup
if let Ok(home_dir) = choose_app_strategy(crate::APP_STRATEGY.clone()) {
let usage = self.agent.usage().await;
log_usage(
home_dir,
self.session_file.to_string_lossy().to_string(),
usage,
);
println!(
"\nClosing session. Recorded to {}",
self.session_file.display()
);
}
println!(
"\nClosing session. Recorded to {}",
self.session_file.display()
);
Ok(())
}

Expand Down Expand Up @@ -645,8 +633,18 @@ impl Session {
self.messages.clone()
}

/// Get the token usage from the agent
pub async fn get_usage(&self) -> Result<Vec<ProviderUsage>> {
Ok(self.agent.usage().await)
/// Get the session metadata
pub fn get_metadata(&self) -> Result<session::SessionMetadata> {
if !self.session_file.exists() {
return Err(anyhow::anyhow!("Session file does not exist"));
}

session::read_metadata(&self.session_file)
}

// Get the session's total token usage
pub fn get_total_token_usage(&self) -> Result<Option<i32>> {
let metadata = self.get_metadata()?;
Ok(metadata.total_tokens)
}
}
5 changes: 1 addition & 4 deletions crates/goose/src/agents/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::sync::Arc;

use super::extension::{ExtensionConfig, ExtensionResult};
use crate::message::Message;
use crate::providers::base::{Provider, ProviderUsage};
use crate::providers::base::Provider;
use crate::session;
use mcp_core::prompt::Prompt;
use mcp_core::protocol::GetPromptResult;
Expand Down Expand Up @@ -47,9 +47,6 @@ pub trait Agent: Send + Sync {
/// Pass through a JSON-RPC request to a specific extension
async fn passthrough(&self, extension: &str, request: Value) -> ExtensionResult<Value>;

/// Get the total usage of the agent
async fn usage(&self) -> Vec<ProviderUsage>;

/// Add custom text to be included in the system prompt
async fn extend_system_prompt(&mut self, extension: String);

Expand Down
33 changes: 1 addition & 32 deletions crates/goose/src/agents/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tracing::{debug, instrument};
use super::extension::{ExtensionConfig, ExtensionError, ExtensionInfo, ExtensionResult};
use crate::config::Config;
use crate::prompt_template;
use crate::providers::base::{Provider, ProviderUsage};
use crate::providers::base::Provider;
use mcp_client::client::{ClientCapabilities, ClientInfo, McpClient, McpClientTrait};
use mcp_client::transport::{SseTransport, StdioTransport, Transport};
use mcp_core::{prompt::Prompt, Content, Tool, ToolCall, ToolError, ToolResult};
Expand All @@ -32,7 +32,6 @@ pub struct Capabilities {
instructions: HashMap<String, String>,
resource_capable_extensions: HashSet<String>,
provider: Arc<Box<dyn Provider>>,
provider_usage: Mutex<Vec<ProviderUsage>>,
system_prompt_override: Option<String>,
system_prompt_extensions: Vec<String>,
}
Expand Down Expand Up @@ -92,7 +91,6 @@ impl Capabilities {
instructions: HashMap::new(),
resource_capable_extensions: HashSet::new(),
provider: Arc::new(provider),
provider_usage: Mutex::new(Vec::new()),
system_prompt_override: None,
system_prompt_extensions: Vec::new(),
}
Expand Down Expand Up @@ -207,12 +205,6 @@ impl Capabilities {
Arc::clone(&self.provider)
}

/// Record provider usage
// TODO consider moving this off to the provider or as a form of logging
pub async fn record_usage(&self, usage: ProviderUsage) {
self.provider_usage.lock().await.push(usage);
}

/// Get aggregated usage statistics
pub async fn remove_extension(&mut self, name: &str) -> ExtensionResult<()> {
let sanitized_name = normalize(name.to_string());
Expand All @@ -227,29 +219,6 @@ impl Capabilities {
Ok(self.clients.keys().cloned().collect())
}

pub async fn get_usage(&self) -> Vec<ProviderUsage> {
let provider_usage = self.provider_usage.lock().await.clone();
let mut usage_map: HashMap<String, ProviderUsage> = HashMap::new();

provider_usage.iter().for_each(|usage| {
usage_map
.entry(usage.model.clone())
.and_modify(|e| {
e.usage.input_tokens = Some(
e.usage.input_tokens.unwrap_or(0) + usage.usage.input_tokens.unwrap_or(0),
);
e.usage.output_tokens = Some(
e.usage.output_tokens.unwrap_or(0) + usage.usage.output_tokens.unwrap_or(0),
);
e.usage.total_tokens = Some(
e.usage.total_tokens.unwrap_or(0) + usage.usage.total_tokens.unwrap_or(0),
);
})
.or_insert_with(|| usage.clone());
});
usage_map.into_values().collect()
}

/// Get all tools from all clients with proper prefixing
pub async fn get_prefixed_tools(&mut self) -> ExtensionResult<Vec<Tool>> {
let mut tools = Vec::new();
Expand Down
7 changes: 0 additions & 7 deletions crates/goose/src/agents/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::agents::capabilities::Capabilities;
use crate::agents::extension::{ExtensionConfig, ExtensionResult};
use crate::message::{Message, ToolRequest};
use crate::providers::base::Provider;
use crate::providers::base::ProviderUsage;
use crate::token_counter::TokenCounter;
use crate::{register_agent, session};
use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -146,7 +145,6 @@ impl Agent for ReferenceAgent {
&messages,
&tools,
).await?;
capabilities.record_usage(usage.clone()).await;

// record usage for the session in the session file
if let Some(session) = session.clone() {
Expand Down Expand Up @@ -204,11 +202,6 @@ impl Agent for ReferenceAgent {
}))
}

async fn usage(&self) -> Vec<ProviderUsage> {
let capabilities = self.capabilities.lock().await;
capabilities.get_usage().await
}

async fn extend_system_prompt(&mut self, extension: String) {
let mut capabilities = self.capabilities.lock().await;
capabilities.add_system_prompt_extension(extension);
Expand Down
8 changes: 0 additions & 8 deletions crates/goose/src/agents/summarize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use crate::config::Config;
use crate::memory_condense::condense_messages;
use crate::message::{Message, ToolRequest};
use crate::providers::base::Provider;
use crate::providers::base::ProviderUsage;
use crate::providers::errors::ProviderError;
use crate::register_agent;
use crate::session;
Expand Down Expand Up @@ -243,8 +242,6 @@ impl Agent for SummarizeAgent {
&tools,
).await {
Ok((response, usage)) => {
capabilities.record_usage(usage.clone()).await;

// record usage for the session in the session file
if let Some(session) = session.clone() {
// TODO: track session_id in langfuse tracing
Expand Down Expand Up @@ -419,11 +416,6 @@ impl Agent for SummarizeAgent {
}))
}

async fn usage(&self) -> Vec<ProviderUsage> {
let capabilities = self.capabilities.lock().await;
capabilities.get_usage().await
}

async fn extend_system_prompt(&mut self, extension: String) {
let mut capabilities = self.capabilities.lock().await;
capabilities.add_system_prompt_extension(extension);
Expand Down
8 changes: 0 additions & 8 deletions crates/goose/src/agents/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::agents::ToolPermissionStore;
use crate::config::Config;
use crate::message::{Message, ToolRequest};
use crate::providers::base::Provider;
use crate::providers::base::ProviderUsage;
use crate::providers::errors::ProviderError;
use crate::providers::toolshim::{
augment_message_with_tool_calls, modify_system_prompt_for_tool_json, OllamaInterpreter,
Expand Down Expand Up @@ -258,8 +257,6 @@ impl Agent for TruncateAgent {
response = augment_message_with_tool_calls(&interpreter, response, &toolshim_tools).await?;
}

capabilities.record_usage(usage.clone()).await;

// record usage for the session in the session file
if let Some(session) = session.clone() {
// TODO: track session_id in langfuse tracing
Expand Down Expand Up @@ -473,11 +470,6 @@ impl Agent for TruncateAgent {
}))
}

async fn usage(&self) -> Vec<ProviderUsage> {
let capabilities = self.capabilities.lock().await;
capabilities.get_usage().await
}

async fn extend_system_prompt(&mut self, extension: String) {
let mut capabilities = self.capabilities.lock().await;
capabilities.add_system_prompt_extension(extension);
Expand Down
Loading