octomind 0.17.0

Session-based AI development assistant with conversational codebase interaction, multimodal vision support, built-in MCP tools, and multi-provider AI integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
// Copyright 2025 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Tool result processor module - handles tool result processing, caching, and follow-up API calls

use crate::config::Config;
use crate::mcp::get_available_functions;
use crate::session::chat::animation::show_smart_animation;
use crate::session::chat::session::ChatSession;
use crate::session::estimate_full_context_tokens;
use crate::session::ChatCompletionWithValidationParams;
use crate::{log_debug, log_info};
use anyhow::Result;
use colored::Colorize;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

// Process tool results and handle follow-up API calls
pub async fn process_tool_results(
	tool_results: Vec<crate::mcp::McpToolResult>,
	total_tool_time_ms: u64,
	chat_session: &mut ChatSession,
	config: &Config,
	role: &str,
	operation_cancelled: tokio::sync::watch::Receiver<bool>,
) -> Result<
	Option<(
		String,
		crate::session::ProviderExchange,
		Option<Vec<crate::mcp::McpToolCall>>,
		Option<String>,                          // response_id from follow-up API call
		Option<crate::providers::ThinkingBlock>, // thinking from follow-up API call
	)>,
> {
	// Add the accumulated tool execution time to the session total
	chat_session.session.info.total_tool_time_ms += total_tool_time_ms;

	// Check for cancellation before making another request
	if *operation_cancelled.borrow() {
		crate::log_debug!("Operation cancelled by user.");
		// Do NOT add any confusing message to the session
		return Ok(None);
	}

	// Create separate animation flag but monitor global cancellation
	let animation_cancel = Arc::new(AtomicBool::new(false));

	// Set up monitor task to propagate global cancellation to animation
	let animation_cancel_monitor = animation_cancel.clone();
	let operation_cancelled_monitor = operation_cancelled.clone();
	let _cancel_monitor = tokio::spawn(async move {
		while !animation_cancel_monitor.load(Ordering::SeqCst) {
			if *operation_cancelled_monitor.borrow() {
				animation_cancel_monitor.store(true, Ordering::SeqCst);
				break;
			}
			tokio::time::sleep(tokio::time::Duration::from_millis(5)).await;
		}
	});

	// 🎯 SENIOR FIX: Show "Generating response..." IMMEDIATELY after tools complete
	// This provides instant feedback while tool results are being processed
	let animation_cancel_flag = animation_cancel.clone();
	let current_cost = chat_session.session.info.total_cost;
	let max_threshold = config.max_session_tokens_threshold;

	// Calculate actual current context tokens for percentage display
	let (_, _, _, _, system_prompt) = config.get_role_config(role);
	let tools = get_available_functions(config).await;
	let current_context_tokens = estimate_full_context_tokens(
		&chat_session.session.messages,
		Some(system_prompt),
		Some(&tools),
	) as u64;

	let animation_task = tokio::spawn(async move {
		let _ = show_smart_animation(
			animation_cancel_flag,
			current_cost,
			current_context_tokens,
			max_threshold,
		)
		.await;
	});

	// 🔍 PERFORMANCE DEBUG: Track where time is spent during tool result processing
	let processing_start = std::time::Instant::now();

	// IMPROVED APPROACH: Add tool results as proper "tool" role messages
	// This follows the standard OpenAI/Anthropic format and avoids double-serialization
	// CRITICAL FIX: Check cache threshold after EACH tool result, not after all
	let cache_manager = crate::session::cache::CacheManager::new();
	let supports_caching = crate::session::model_supports_caching(&chat_session.model);

	let mut cache_check_time = 0u128;
	let mut truncation_time = 0u128;

	// PERFORMANCE OPTIMIZATION: Batch process tool results with smart truncation
	// Instead of checking truncation after EVERY tool result (expensive),
	// we batch process and only truncate when necessary or at the end
	let mut accumulated_content_size = 0;
	let mut needs_truncation_check = false;

	for tool_result in &tool_results {
		// CRITICAL FIX: Extract ONLY the actual tool output, not our custom JSON wrapper
		let tool_content = extract_tool_content(tool_result);

		// PERFORMANCE OPTIMIZATION: Check size before moving content
		let content_size = tool_content.len();
		accumulated_content_size += content_size;
		let is_large_output = content_size > 10000; // 10KB+ outputs
		let accumulated_is_large = accumulated_content_size > 50000; // 50KB+ total

		if is_large_output || accumulated_is_large {
			needs_truncation_check = true;
		}

		// Use the new add_tool_message method which handles token tracking properly
		chat_session.add_tool_message(
			&tool_content,
			&tool_result.tool_id,
			&tool_result.tool_name,
			config,
		)?;

		// Check truncation only for large individual tool outputs (file contents, search results, etc.)
		if is_large_output {
			let truncation_start = std::time::Instant::now();
			if let Err(e) = crate::session::chat::context_truncation::check_and_truncate_context(
				chat_session,
				config,
				crate::session::chat::TruncationOptions {
					defer_continuation: true, // Defer during tool processing
				},
			)
			.await
			{
				log_info!("Warning: Error during tool result truncation check: {}", e);
			}
			truncation_time += truncation_start.elapsed().as_millis();

			// Reset flags after truncation
			needs_truncation_check = false;
			accumulated_content_size = 0;
		}
	}

	// 🗜️ PLAN-DRIVEN COMPRESSION: Process any pending compression requests
	// This happens after tool results are added but before continuation check
	// Compression can significantly reduce context, potentially avoiding continuation

	// Check if any plan tool was executed and set message range if needed
	let mut plan_tool_executed = false;
	for tool_result in &tool_results {
		if tool_result.tool_name == "plan" {
			plan_tool_executed = true;
			break;
		}
	}

	// If plan tool was executed, set the message range for compression
	// Only attempt this when compression is actually pending
	if plan_tool_executed && crate::mcp::dev::plan::has_pending_compression() {
		// Get the start index from the global state (set by session before tool execution)
		if let Some(start_index) = crate::mcp::dev::plan::core::get_and_clear_start_index() {
			// Use last valid index (len - 1) since remove_messages_in_range uses inclusive end_index
			// If we used len(), it would be out of bounds for the inclusive range operation
			let end_index = chat_session.get_message_count() - 1;
			// Set the message range on the pending compression task
			if let Err(e) =
				crate::mcp::dev::plan::set_pending_compression_range(start_index, end_index)
			{
				log_info!(
					"Failed to set compression range: {}. Compression will be skipped.",
					e
				);
			}
		} else {
			log_info!(
				"Plan tool executed but no start index found for compression. \
					This may indicate the start index was not set before tool execution."
			);
		}
	}

	// Process any pending compression with proper error handling
	let task_compression_occurred =
		match crate::mcp::dev::plan::process_pending_compression(chat_session).await {
			Ok(Some(metrics)) => {
				chat_session
					.session
					.info
					.compression_stats
					.add_task_compression(metrics.messages_removed, metrics.tokens_saved);
				crate::session::chat::cost_tracker::CostTracker::display_compression_result(
					"Task", &metrics,
				);
				true
			}
			Ok(None) => {
				// No pending compression - this is normal
				false
			}
			Err(e) => {
				// Compression failed - log at INFO level since this is unexpected
				log_info!(
					"❌ Task compression failed: {}. Context was not compressed.",
					e
				);
				// Note: Session continues normally - compression is best-effort
				false
			}
		};

	// CRITICAL FIX: After task compression, check if continuation should trigger
	// This allows automatic continuation after single-task compression
	if task_compression_occurred && crate::mcp::dev::plan::core::has_active_plan() {
		log_debug!("Task compression completed with active plan - checking if continuation needed");
		use crate::session::chat::session_continuation;
		if session_continuation::check_and_handle_continuation(chat_session, config).await? {
			// Stop animation before returning
			animation_cancel.store(true, Ordering::SeqCst);
			let _ = animation_task.await;

			log_info!("Continuation triggered after task compression - returning to main loop");
			return Ok(None);
		}
	}

	// Process phase compression (automatic)
	let phase_compression_occurred =
		match crate::mcp::dev::plan::process_pending_phase_compression(chat_session).await {
			Ok(Some(metrics)) => {
				chat_session
					.session
					.info
					.compression_stats
					.add_phase_compression(metrics.messages_removed, metrics.tokens_saved);
				crate::session::chat::cost_tracker::CostTracker::display_compression_result(
					"Phase", &metrics,
				);
				true
			}
			Ok(None) => false,
			Err(e) => {
				log_info!(
					"❌ Phase compression failed: {}. Context was not compressed.",
					e
				);
				false
			}
		};

	// CRITICAL FIX: After phase compression, check if continuation should trigger
	// Phase compression can free significant space, allowing plan to continue automatically
	if phase_compression_occurred && crate::mcp::dev::plan::core::has_active_plan() {
		log_debug!(
			"Phase compression completed with active plan - checking if continuation needed"
		);
		use crate::session::chat::session_continuation;
		if session_continuation::check_and_handle_continuation(chat_session, config).await? {
			// Stop animation before returning
			animation_cancel.store(true, Ordering::SeqCst);
			let _ = animation_task.await;

			log_info!("Continuation triggered after phase compression - returning to main loop");
			return Ok(None);
		}
	}
	// Process project compression (automatic)
	let project_compression_occurred =
		match crate::mcp::dev::plan::process_pending_project_compression(chat_session).await {
			Ok(Some(metrics)) => {
				chat_session
					.session
					.info
					.compression_stats
					.add_project_compression(metrics.messages_removed, metrics.tokens_saved);
				crate::session::chat::cost_tracker::CostTracker::display_compression_result(
					"Project", &metrics,
				);
				true
			}
			Ok(None) => false,
			Err(e) => {
				log_info!(
					"❌ Project compression failed: {}. Context was not compressed.",
					e
				);
				false
			}
		};

	// CRITICAL FIX: After project compression, check if continuation should trigger
	// Project compression can free significant space, allowing plan to continue automatically
	// This matches the behavior in main_loop.rs for conversation compression (lines 603-617)
	if project_compression_occurred && crate::mcp::dev::plan::core::has_active_plan() {
		log_debug!(
			"Project compression completed with active plan - checking if continuation needed"
		);
		use crate::session::chat::session_continuation;
		if session_continuation::check_and_handle_continuation(chat_session, config).await? {
			// Stop animation before returning
			animation_cancel.store(true, Ordering::SeqCst);
			let _ = animation_task.await;

			log_info!("Continuation triggered after project compression - returning to main loop");
			// Return None to signal main loop to continue with the injected continuation request
			return Ok(None);
		}
	}

	// BATCH TRUNCATION: Check once after all small tool results are processed
	if needs_truncation_check {
		let truncation_start = std::time::Instant::now();
		if let Err(e) = crate::session::chat::context_truncation::check_and_truncate_context(
			chat_session,
			config,
			crate::session::chat::TruncationOptions {
				defer_continuation: true, // Defer during tool processing
			},
		)
		.await
		{
			log_info!("Warning: Error during batch truncation check: {}", e);
		}
		truncation_time += truncation_start.elapsed().as_millis();
	}

	// 🗜️ ADAPTIVE CONVERSATION COMPRESSION: Check if context should be compressed
	// CRITICAL: This must happen DURING tool execution loops, not just on user requests
	// Context can balloon from 50K → 200K+ tokens during multi-tool execution without this check
	// This runs AFTER plan compression (which handles structured plan content)
	// and BEFORE continuation (compression can prevent hitting continuation threshold)
	if let Err(e) = crate::session::chat::conversation_compression::check_and_compress_conversation(
		chat_session,
		config,
	)
	.await
	{
		// Best-effort: log error but continue session
		log_debug!(
			"Adaptive conversation compression failed during tool processing: {}. Continuing session.",
			e
		);
	}

	// NEW FLOW: Check for continuation AFTER all tool results are gathered
	// This is one of the two correct moments to trigger continuation:
	// 1) On new user request (handled in session runner)
	// 2) After all tool results gathered, before sending to AI (HERE)
	use crate::session::chat::session_continuation;
	if session_continuation::check_and_handle_continuation(chat_session, config).await? {
		// Stop animation before returning
		animation_cancel.store(true, Ordering::SeqCst);
		let _ = animation_task.await;

		log_info!("Token limit reached after processing all tool results - continuation triggered");
		return Ok(None); // Return None to stop tool processing and let continuation be handled by main loop
	}

	// CRITICAL FIX: Check cache threshold AFTER all tool results are processed
	// This ensures cache markers are set at the correct boundary - after all parallel
	// tool results are added to session, but before sending the complete batch to server
	let cache_start = std::time::Instant::now();
	if let Ok(true) = cache_manager.check_and_apply_auto_cache_threshold(
		&mut chat_session.session,
		config,
		supports_caching,
		role,
	) {
		log_debug!("Auto-cache threshold reached after processing all tool results - cache checkpoint applied before follow-up API request.");
	}
	cache_check_time += cache_start.elapsed().as_millis();

	// 🔍 PERFORMANCE DEBUG: Report processing breakdown and track processing time
	let total_processing_time = processing_start.elapsed().as_millis() as u64;

	// Add the processing time to the session total
	chat_session.session.info.total_layer_time_ms += total_processing_time;

	if total_processing_time > 100 {
		log_debug!(
			"🔍 Tool result processing took {}ms (cache: {}ms, truncation: {}ms)",
			total_processing_time,
			cache_check_time,
			truncation_time
		);
	}

	// Check spending threshold before making follow-up API call
	match chat_session.check_spending_threshold(config) {
		Ok(should_continue) => {
			if !should_continue {
				// User chose not to continue due to spending threshold
				animation_cancel.store(true, Ordering::SeqCst);
				let _ = animation_task.await;
				println!(
					"{}",
					"✗ Tool follow-up cancelled due to spending threshold.".bright_red()
				);
				return Ok(None);
			}
		}
		Err(e) => {
			// Error checking threshold, log warning and continue
			use colored::*;
			println!(
				"{}: {}",
				"Warning: Error checking spending threshold".bright_yellow(),
				e
			);
		}
	}

	// Check request spending threshold before making follow-up API call
	match chat_session.check_request_spending_threshold(config) {
		Ok(should_continue) => {
			if !should_continue {
				// Request spending threshold exceeded - stop execution
				animation_cancel.store(true, Ordering::SeqCst);
				let _ = animation_task.await;
				println!(
					"{}",
					"✗ Tool follow-up cancelled due to request spending threshold.".bright_red()
				);
				return Ok(None);
			}
		}
		Err(e) => {
			// Error checking request threshold, log warning and continue
			use colored::*;
			println!(
				"{}: {}",
				"Warning: Error checking request spending threshold".bright_yellow(),
				e
			);
		}
	}

	// CRITICAL FIX: Check for cancellation before making follow-up API call
	if *operation_cancelled.borrow() {
		// Stop animation before returning
		animation_cancel.store(true, Ordering::SeqCst);
		let _ = animation_task.await;
		crate::log_debug!("Operation cancelled by user.");
		return Ok(None);
	}

	// Make follow-up API call
	let follow_up_result =
		make_follow_up_api_call(chat_session, config, operation_cancelled.clone()).await;

	// Stop the animation and wait for completion
	animation_cancel.store(true, Ordering::SeqCst);
	let _ = animation_task.await;

	// Show cost breakdown for intermediate results (after tool calls, before follow-up AI call)
	// Always show simple cost line, detailed breakdown only at info log level
	use crate::session::chat::cost_tracker::CostTracker;
	CostTracker::display_intermediate_cost_breakdown(chat_session);

	match follow_up_result {
		Ok(response) => {
			// Store direct tool calls for efficient processing if they exist
			let has_more_tools = if let Some(ref calls) = response.tool_calls {
				!calls.is_empty()
			} else {
				// Fall back to parsing if no direct tool calls
				!crate::mcp::parse_tool_calls(&response.content).is_empty()
			};

			// Debug logging for follow-up finish_reason
			if let Some(ref reason) = response.finish_reason {
				log_debug!("Follow-up finish_reason: {}", reason);
			}

			// Check finish_reason to determine if we should continue the conversation
			let should_continue_conversation =
				check_should_continue(&response, config, has_more_tools);

			// Handle cost tracking from follow-up API call
			handle_follow_up_cost_tracking(chat_session, &response.exchange, config);

			// Display rate limit information if available
			display_rate_limit_info(&response.exchange);

			if should_continue_conversation {
				Ok(Some((
					response.content,
					response.exchange,
					response.tool_calls,
					response.response_id, // Include response_id from follow-up response
					response.thinking,    // CRITICAL FIX: Include thinking from follow-up response for Moonshot
				)))
			} else {
				// If no more tools, return None to break out of loop
				Ok(Some((
					response.content,
					response.exchange,
					None,
					response.response_id,
					response.thinking, // CRITICAL FIX: Include thinking even when stopping
				)))
			}
		}
		Err(e) => {
			// Extract provider name from the model for better error messaging
			let provider_name = if let Ok((provider, _)) =
				crate::providers::ProviderFactory::parse_model(&chat_session.model)
			{
				provider
			} else {
				"unknown provider".to_string()
			};

			// IMPROVED: Show provider-aware context about the API error
			let error_message =
				crate::session::chat::session::format_provider_error(&provider_name, &e);
			println!(
				"\n{} {}: {}",
				"".bright_red(),
				format!("Error calling {}", provider_name).bright_red(),
				error_message
			);

			// Additional context if error contains provider information
			log_debug!("Model: {}", chat_session.model);
			log_debug!("Temperature: {}", chat_session.temperature);

			Err(e)
		}
	}
}

// Extract tool content from tool result
fn extract_tool_content(tool_result: &crate::mcp::McpToolResult) -> String {
	if let Some(output) = tool_result.result.get("output") {
		// Extract the "output" field which contains the actual tool result
		if let Some(output_str) = output.as_str() {
			output_str.to_string()
		} else {
			// If output is not a string, serialize it
			serde_json::to_string(output).unwrap_or_default()
		}
	} else if tool_result.result.is_string() {
		// If result is already a string, use it directly
		tool_result.result.as_str().unwrap_or("").to_string()
	} else {
		// Fallback: look for common fields or use the whole result
		if let Some(error) = tool_result.result.get("error") {
			format!("Error: {}", error)
		} else {
			// Last resort: serialize the whole result
			serde_json::to_string(&tool_result.result).unwrap_or_default()
		}
	}
}

// Make follow-up API call with cancellation support
async fn make_follow_up_api_call(
	chat_session: &ChatSession,
	config: &Config,
	cancellation_token: tokio::sync::watch::Receiver<bool>,
) -> Result<crate::providers::ProviderResponse> {
	let model = chat_session.model.clone();
	let temperature = chat_session.temperature;

	// CRITICAL FIX: Pass cancellation token to ensure immediate cancellation
	let validation_params = ChatCompletionWithValidationParams::new(
		&chat_session.session.messages,
		&model,
		temperature,
		chat_session.top_p,
		chat_session.top_k,
		chat_session.max_tokens,
		config,
	)
	.with_max_retries(chat_session.max_retries)
	.with_cancellation_token(cancellation_token);
	crate::session::chat_completion_with_validation(validation_params).await
}

// Check if conversation should continue based on finish_reason
pub fn check_should_continue(
	response: &crate::providers::ProviderResponse,
	_config: &Config,
	has_more_tools: bool,
) -> bool {
	match response.finish_reason.as_deref() {
		Some("tool_calls") | Some("tool_use") => {
			// Model wants to make more tool calls
			log_debug!("finish_reason is 'tool_calls', continuing conversation");
			true
		}
		Some("stop") | Some("length") | Some("end_turn") => {
			// Model finished normally or hit length limit
			log_debug!(
				"finish_reason is '{}', ending conversation",
				response.finish_reason.as_deref().unwrap()
			);
			false
		}
		Some(other) => {
			// Unknown finish_reason, be conservative and continue
			log_info!("Unknown finish_reason '{}', continuing conversation", other);
			true
		}
		None => {
			// No finish_reason, check for tool calls
			log_debug!("Debug: No finish_reason, checking for tool calls");
			has_more_tools
		}
	}
}

// Handle cost tracking from follow-up API call
fn handle_follow_up_cost_tracking(
	chat_session: &mut ChatSession,
	exchange: &crate::session::ProviderExchange,
	_config: &Config,
) {
	if let Some(usage) = &exchange.usage {
		// Simple token extraction with clean provider interface
		let cached_tokens = usage.cached_tokens;
		let regular_prompt_tokens = usage.prompt_tokens.saturating_sub(cached_tokens);

		// Update session token counts using the cache manager
		let cache_manager = crate::session::cache::CacheManager::new();
		cache_manager.update_token_tracking(
			&mut chat_session.session,
			regular_prompt_tokens,
			usage.output_tokens,
			cached_tokens,
		);

		// Track API time from the follow-up exchange
		if let Some(api_time_ms) = usage.request_time_ms {
			chat_session.session.info.total_api_time_ms += api_time_ms;
		}

		// Update cost
		if let Some(cost) = usage.cost {
			// OpenRouter credits = dollars, use the value directly
			chat_session.session.info.total_cost += cost;
			chat_session.estimated_cost = chat_session.session.info.total_cost;

			log_debug!(
				"Adding ${:.5} to total cost (total now: ${:.5})",
				cost,
				chat_session.session.info.total_cost
			);

			// CRITICAL: Log session stats immediately after cost update
			let _ = crate::session::logger::log_session_stats(
				&chat_session.session.info.name,
				&chat_session.session.info,
			);

			// Enhanced debug for follow-up calls
			log_debug!("Tool response usage detail:");
			if let Ok(usage_str) = serde_json::to_string_pretty(usage) {
				log_debug!("{}", usage_str);
			}

			// Check for cache-related fields
			if let Some(raw_usage) = exchange.response.get("usage") {
				log_debug!("Raw tool response usage object:");
				if let Ok(raw_str) = serde_json::to_string_pretty(raw_usage) {
					log_debug!("{}", raw_str);
				}

				// Look specifically for cache-related fields
				if let Some(cache_cost) = raw_usage.get("cache_cost") {
					log_debug!("Found cache_cost field: {}", cache_cost);
				}

				if let Some(cached_cost) = raw_usage.get("cached_cost") {
					log_debug!("Found cached_cost field: {}", cached_cost);
				}

				if let Some(any_cache) = raw_usage.get("cached") {
					log_debug!("Found cached field: {}", any_cache);
				}
			}
		} else {
			// Try to get cost from the raw response if not in usage struct
			let cost_from_raw = exchange
				.response
				.get("usage")
				.and_then(|u| u.get("cost"))
				.and_then(|c| c.as_f64());

			if let Some(cost) = cost_from_raw {
				// Use the cost value directly
				chat_session.session.info.total_cost += cost;
				chat_session.estimated_cost = chat_session.session.info.total_cost;

				log_debug!(
					"Using cost ${:.5} from raw response (total now: ${:.5})",
					cost,
					chat_session.session.info.total_cost
				);

				// CRITICAL: Log session stats immediately after cost update
				let _ = crate::session::logger::log_session_stats(
					&chat_session.session.info.name,
					&chat_session.session.info,
				);
			} else {
				// Only show error if no cost data found
				let provider_name = &exchange.provider;
				println!(
					"{} {} {}",
					"ERROR:".bright_red(),
					provider_name.bright_yellow(),
					"did not provide cost data for tool response API call".bright_red()
				);

				// Check if usage tracking was explicitly requested (OpenRouter-specific)
				if provider_name == "openrouter" {
					let has_usage_flag = exchange
						.request
						.get("usage")
						.and_then(|u| u.get("include"))
						.and_then(|i| i.as_bool())
						.unwrap_or(false);

					println!(
						"{} {}",
						"Request had usage.include flag:".bright_yellow(),
						has_usage_flag
					);
					if !has_usage_flag {
						println!(
							"{}",
							"Make sure usage.include=true is set for OpenRouter!".bright_yellow()
						);
					}
				}

				// Dump the raw response for debugging
				log_debug!("Raw {} response for debugging:", provider_name);
				if let Ok(resp_str) = serde_json::to_string_pretty(&exchange.response) {
					log_debug!("Partial response JSON:\n{}", resp_str);
				}
			}
		}
	} else {
		println!(
			"{}",
			"ERROR: No usage data for tool response API call".bright_red()
		);
	}
}

// Helper function to display rate limit information from provider response
fn display_rate_limit_info(exchange: &crate::session::ProviderExchange) {
	if let Some(ref rate_limit_headers) = exchange.rate_limit_headers {
		let mut rate_limit_info = Vec::new();

		match exchange.provider.as_str() {
			"anthropic" => {
				// Anthropic rate limit format
				if let (Some(tokens_remaining), Some(tokens_limit)) = (
					rate_limit_headers.get("tokens_remaining"),
					rate_limit_headers.get("tokens_limit"),
				) {
					rate_limit_info.push(format!("Tokens: {}/{}", tokens_remaining, tokens_limit));
				}

				if let (Some(input_remaining), Some(input_limit)) = (
					rate_limit_headers.get("input_tokens_remaining"),
					rate_limit_headers.get("input_tokens_limit"),
				) {
					rate_limit_info
						.push(format!("Input tokens: {}/{}", input_remaining, input_limit));
				}

				if let (Some(output_remaining), Some(output_limit)) = (
					rate_limit_headers.get("output_tokens_remaining"),
					rate_limit_headers.get("output_tokens_limit"),
				) {
					rate_limit_info.push(format!(
						"Output tokens: {}/{}",
						output_remaining, output_limit
					));
				}

				if !rate_limit_info.is_empty() {
					crate::log_info!("📊 Anthropic rate limits: {}", rate_limit_info.join(" | "));
				}
			}
			"openai" => {
				// OpenAI rate limit format
				if let (Some(requests_remaining), Some(requests_limit)) = (
					rate_limit_headers.get("requests_remaining"),
					rate_limit_headers.get("requests_limit"),
				) {
					rate_limit_info.push(format!(
						"Requests: {}/{}",
						requests_remaining, requests_limit
					));
				}

				if let (Some(tokens_remaining), Some(tokens_limit)) = (
					rate_limit_headers.get("tokens_remaining"),
					rate_limit_headers.get("tokens_limit"),
				) {
					rate_limit_info.push(format!("Tokens: {}/{}", tokens_remaining, tokens_limit));
				}

				if let Some(request_reset) = rate_limit_headers.get("request_reset") {
					rate_limit_info.push(format!("Request reset: {}", request_reset));
				}

				if !rate_limit_info.is_empty() {
					crate::log_info!("📊 OpenAI rate limits: {}", rate_limit_info.join(" | "));
				}
			}
			_ => {
				// Generic rate limit display for other providers
				if !rate_limit_headers.is_empty() {
					let info: Vec<String> = rate_limit_headers
						.iter()
						.map(|(k, v)| format!("{}: {}", k, v))
						.collect();
					crate::log_info!("📊 {} rate limits: {}", exchange.provider, info.join(" | "));
				}
			}
		}
	}
}