Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix: Address code review comments from djpetti

LearningCircuit09adf7ea8c90
diff --git a/src/local_deep_research/database/models/metrics.py b/src/local_deep_research/database/models/metrics.py
index 6a9d7cd379..5a5680f3a2 100644
--- a/src/local_deep_research/database/models/metrics.py
+++ b/src/local_deep_research/database/models/metrics.py
@@ -4,6 +4,7 @@ Metrics and usage tracking models.
from sqlalchemy import (
JSON,
+ Boolean,
Column,
Float,
Integer,
@@ -61,8 +62,8 @@ class TokenUsage(Base):
# Context overflow detection columns
context_limit = Column(Integer) # The configured num_ctx or max tokens
context_truncated = Column(
- Integer, default=0
- ) # 1 if truncation detected, 0 otherwise
+ Boolean, default=False
+ ) # True if request was truncated due to context limit
tokens_truncated = Column(Integer) # Estimated tokens lost to truncation
truncation_ratio = Column(Float) # Percentage of prompt that was truncated
@@ -71,12 +72,18 @@ class TokenUsage(Base):
Integer
) # Raw prompt_eval_count from Ollama
ollama_eval_count = Column(Integer) # Raw eval_count from Ollama
- ollama_total_duration = Column(Integer) # Total time in nanoseconds
- ollama_load_duration = Column(Integer) # Model load time in nanoseconds
+ ollama_total_duration = Column(
+ Integer
+ ) # Total time in nanoseconds (raw from Ollama API)
+ ollama_load_duration = Column(
+ Integer
+ ) # Model load time in nanoseconds (raw from Ollama API)
ollama_prompt_eval_duration = Column(
Integer
- ) # Prompt eval time in nanoseconds
- ollama_eval_duration = Column(Integer) # Generation time in nanoseconds
+ ) # Prompt eval time in nanoseconds (raw from Ollama API)
+ ollama_eval_duration = Column(
+ Integer
+ ) # Generation time in nanoseconds (raw from Ollama API)
def __repr__(self):
return f"<TokenUsage(model={self.model_name}, total_tokens={self.total_tokens}, cost=${self.total_cost:.4f})>"
diff --git a/src/local_deep_research/database/thread_metrics.py b/src/local_deep_research/database/thread_metrics.py
index 23c22abc40..ef9f3e4fe8 100644
--- a/src/local_deep_research/database/thread_metrics.py
+++ b/src/local_deep_research/database/thread_metrics.py
@@ -120,7 +120,7 @@ class ThreadSafeMetricsWriter:
call_stack=token_data.get("call_stack"),
# Context overflow detection
context_limit=token_data.get("context_limit"),
- context_truncated=token_data.get("context_truncated", 0),
+ context_truncated=token_data.get("context_truncated", False),
tokens_truncated=token_data.get("tokens_truncated"),
truncation_ratio=token_data.get("truncation_ratio"),
# Raw Ollama metrics
diff --git a/src/local_deep_research/metrics/token_counter.py b/src/local_deep_research/metrics/token_counter.py
index f1ae3d6978..37f7cc1646 100644
--- a/src/local_deep_research/metrics/token_counter.py
+++ b/src/local_deep_research/metrics/token_counter.py
@@ -305,13 +305,16 @@ class TokenCountingCallback(BaseCallbackHandler):
self.original_prompt_estimate
> prompt_eval_count
):
- self.tokens_truncated = (
+ self.tokens_truncated = max(
+ 0,
self.original_prompt_estimate
- - prompt_eval_count
+ - prompt_eval_count,
)
self.truncation_ratio = (
self.tokens_truncated
/ self.original_prompt_estimate
+ if self.original_prompt_estimate > 0
+ else 0
)
logger.warning(
f"Context overflow detected! "
@@ -376,6 +379,30 @@ class TokenCountingCallback(BaseCallbackHandler):
if self.research_id:
self._save_to_db(0, 0)
+ def _get_context_overflow_fields(self) -> Dict[str, Any]:
+ """Get context overflow detection fields for database saving."""
+ return {
+ "context_limit": self.context_limit,
+ "context_truncated": self.context_truncated, # Now Boolean
+ "tokens_truncated": self.tokens_truncated
+ if self.context_truncated
+ else None,
+ "truncation_ratio": self.truncation_ratio
+ if self.context_truncated
+ else None,
+ # Raw Ollama metrics
+ "ollama_prompt_eval_count": self.ollama_metrics.get(
+ "prompt_eval_count"
+ ),
+ "ollama_eval_count": self.ollama_metrics.get("eval_count"),
+ "ollama_total_duration": self.ollama_metrics.get("total_duration"),
+ "ollama_load_duration": self.ollama_metrics.get("load_duration"),
+ "ollama_prompt_eval_duration": self.ollama_metrics.get(
+ "prompt_eval_duration"
+ ),
+ "ollama_eval_duration": self.ollama_metrics.get("eval_duration"),
+ }
+
def _save_to_db(self, prompt_tokens: int, completion_tokens: int):
"""Save token usage to the database."""
# Check if we're in a thread - if so, queue the save for later
@@ -429,32 +456,8 @@ class TokenCountingCallback(BaseCallbackHandler):
"calling_file": self.calling_file,
"calling_function": self.calling_function,
"call_stack": self.call_stack,
- # Context overflow detection
- "context_limit": self.context_limit,
- "context_truncated": 1 if self.context_truncated else 0,
- "tokens_truncated": self.tokens_truncated
- if self.context_truncated
- else None,
- "truncation_ratio": self.truncation_ratio
- if self.context_truncated
- else None,
- # Raw Ollama metrics
- "ollama_prompt_eval_count": self.ollama_metrics.get(
- "prompt_eval_count"
- ),
- "ollama_eval_count": self.ollama_metrics.get("eval_count"),
- "ollama_total_duration": self.ollama_metrics.get(
- "total_duration"
- ),
- "ollama_load_duration": self.ollama_metrics.get(
- "load_duration"
- ),
- "ollama_prompt_eval_duration": self.ollama_metrics.get(
- "prompt_eval_duration"
- ),
- "ollama_eval_duration": self.ollama_metrics.get(
- "eval_duration"
- ),
+ # Add context overflow fields using helper method
+ **self._get_context_overflow_fields(),
}
# Convert list to JSON string if needed
@@ -530,7 +533,7 @@ class TokenCountingCallback(BaseCallbackHandler):
search_engines_planned = json.dumps(search_engines_planned)
# Log context overflow detection values before saving
- logger.info(
+ logger.debug(
f"Saving TokenUsage - context_limit: {self.context_limit}, "
f"context_truncated: {self.context_truncated}, "
f"tokens_truncated: {self.tokens_truncated}, "
@@ -564,32 +567,8 @@ class TokenCountingCallback(BaseCallbackHandler):
calling_file=self.calling_file,
calling_function=self.calling_function,
call_stack=self.call_stack,
- # Context overflow detection
- context_limit=self.context_limit,
- context_truncated=1 if self.context_truncated else 0,
- tokens_truncated=self.tokens_truncated
- if self.context_truncated
- else None,
- truncation_ratio=self.truncation_ratio
- if self.context_truncated
- else None,
- # Raw Ollama metrics
- ollama_prompt_eval_count=self.ollama_metrics.get(
- "prompt_eval_count"
- ),
- ollama_eval_count=self.ollama_metrics.get("eval_count"),
- ollama_total_duration=self.ollama_metrics.get(
- "total_duration"
- ),
- ollama_load_duration=self.ollama_metrics.get(
- "load_duration"
- ),
- ollama_prompt_eval_duration=self.ollama_metrics.get(
- "prompt_eval_duration"
- ),
- ollama_eval_duration=self.ollama_metrics.get(
- "eval_duration"
- ),
+ # Add context overflow fields using helper method
+ **self._get_context_overflow_fields(),
)
session.add(token_usage)
diff --git a/src/local_deep_research/web/routes/context_overflow_api.py b/src/local_deep_research/web/routes/context_overflow_api.py
index f7e90fec54..28e883764d 100644
--- a/src/local_deep_research/web/routes/context_overflow_api.py
+++ b/src/local_deep_research/web/routes/context_overflow_api.py
@@ -57,7 +57,7 @@ def get_context_overflow_metrics():
# Truncated requests
truncated_requests = query.filter(
- TokenUsage.context_truncated == 1
+ TokenUsage.context_truncated.is_(True)
).count()
# Calculate truncation rate
@@ -70,7 +70,7 @@ def get_context_overflow_metrics():
# Get average tokens truncated
avg_tokens_truncated = session.query(
func.avg(TokenUsage.tokens_truncated)
- ).filter(TokenUsage.context_truncated == 1)
+ ).filter(TokenUsage.context_truncated.is_(True))
if start_date:
avg_tokens_truncated = avg_tokens_truncated.filter(
@@ -97,7 +97,7 @@ def get_context_overflow_metrics():
# Get recent truncated requests
recent_truncated = (
- query.filter(TokenUsage.context_truncated == 1)
+ query.filter(TokenUsage.context_truncated.is_(True))
.order_by(desc(TokenUsage.timestamp))
.limit(20)
.all()