~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Shared synchronous dispatch preserves token-usage caller attribution."""
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch
import pytest
from local_deep_research.metrics.token_counter import TokenCountingCallback
_MODULE = "local_deep_research.metrics.token_counter"
def _frame(filename, function, lineno=1):
return SimpleNamespace(filename=filename, function=function, lineno=lineno)
@pytest.mark.parametrize(
"root",
[
"/repo/src/local_deep_research",
"/venv/lib/python3.12/site-packages/local_deep_research",
],
)
@pytest.mark.parametrize("wrapped", [False, True])
def test_dispatch_preserves_caller_and_full_stack_budget(root, wrapped):
callback = TokenCountingCallback(research_id="dispatch-attribution")
caller = _frame(
f"{root}/report_generator.py", "_determine_report_structure", 367
)
dispatch = _frame(f"{root}/utilities/llm_utils.py", "invoke_llm_sync", 47)
wrapper = _frame(f"{root}/config/llm_config.py", "invoke", 425)
outer = [
_frame(f"{root}/runner.py", f"outer_{index}", 100 + index)
for index in range(1, 6)
]
prefix = [
_frame("/callback.py", "on_llm_start"),
_frame("/vendor.py", "invoke"),
]
if wrapped:
prefix.append(wrapper)
with patch(
f"{_MODULE}.inspect.stack", return_value=prefix + [caller] + outer
):
callback.on_llm_start({}, ["synthetic prompt"], run_id="direct")
with patch(
f"{_MODULE}.inspect.stack",
return_value=prefix + [dispatch, caller] + outer,
):
callback.on_llm_start({}, ["synthetic prompt"], run_id="dispatched")
direct = callback._call_meta["direct"]
dispatched = callback._call_meta["dispatched"]
assert dispatched == direct
assert dispatched["calling_file"] == (
"config/llm_config.py" if wrapped else "report_generator.py"
)
assert dispatched["calling_function"] == (
"invoke" if wrapped else "_determine_report_structure"
)
frames = dispatched["call_stack"].split(" -> ")
assert len(frames) == 5
assert "llm_utils.py" not in dispatched["call_stack"]
assert frames[-1] == (
"runner.py:outer_3:103" if wrapped else "runner.py:outer_4:104"
)
@pytest.mark.parametrize(
("relative_path", "function"),
[
("other/llm_utils.py", "invoke_llm_sync"),
("utilities/llm_utils.py", "get_model_identifier"),
],
)
def test_other_functions_and_same_named_modules_keep_attribution(
relative_path, function
):
callback = TokenCountingCallback(research_id="dispatch-negative-control")
frames = [
_frame("/callback.py", "on_llm_start"),
_frame(f"/repo/src/local_deep_research/{relative_path}", function, 10),
_frame(
"/repo/src/local_deep_research/report_generator.py", "generate", 20
),
]
with patch(f"{_MODULE}.inspect.stack", return_value=frames):
callback.on_llm_start({}, ["synthetic prompt"], run_id="other")
metadata = callback._call_meta["other"]
assert metadata["calling_file"] == relative_path
assert metadata["calling_function"] == function
assert metadata["call_stack"].startswith(f"llm_utils.py:{function}:10 -> ")
def test_dispatch_without_a_project_caller_has_no_attribution():
callback = TokenCountingCallback(research_id="dispatch-no-caller")
frames = [
_frame("/callback.py", "on_llm_start"),
_frame(
"/repo/src/local_deep_research/utilities/llm_utils.py",
"invoke_llm_sync",
),
_frame("/external/client.py", "run"),
]
with patch(f"{_MODULE}.inspect.stack", return_value=frames):
callback.on_llm_start({}, ["synthetic prompt"], run_id="external")
assert callback._call_meta["external"] == {
"calling_file": None,
"calling_function": None,
"call_stack": None,
}
def test_llm_utils_module_matches_the_dispatch_frame_filter_literals():
"""The filter in token_counter.py matches frames on
``frame.function == "invoke_llm_sync"`` and
``Path(frame.filename).parts[-3:] == ("local_deep_research",
"utilities", "llm_utils.py")``. This anchors both literals against the
real module, so a rename or move of either fails here instead of only
silently disabling the dispatch-frame filter.
"""
from local_deep_research.utilities import llm_utils
assert Path(llm_utils.__file__).parts[-3:] == (
"local_deep_research",
"utilities",
"llm_utils.py",
)
assert llm_utils.invoke_llm_sync.__name__ == "invoke_llm_sync"