Skip to content

BrainAPI is a knowledge graph–powered AI memory layer that transforms unstructured data into structured knowledge, enabling intelligent search, recommendations, and contextual memory for AI agents and applications.

feat(token logging): removed old agent token counting

Christianbcfae2124dba
diff --git a/.env.example b/.env.example
index e485dcc..e8f133c 100644
--- a/.env.example
+++ b/.env.example
@@ -110,11 +110,6 @@ GCP_CREDENTIALS_PATH=".path-to-credentials-file.json"
# Used to run the worker tasks
CELERY_WORKER_CONCURRENCY=4
-# Pricing
-# Used to calculate the pricing of the LLM calls
-INPUT_TOKEN_PRICE=0.0000005
-OUTPUT_TOKEN_PRICE=0.000003
-
# DocParser
# Used to parse the documents into markdown text
# and by forwarding the webhook (our app host) and the task identifier,
diff --git a/README.md b/README.md
index f389db3..0de1d4a 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
<p align="center">
<a href="https://discord.gg/VTngQTaeDf"><img src="https://img.shields.io/badge/Discord-Join%20Lumen%20Brain-5865F2?style=for-the-badge&logo=discord&logoColor=white" alt="Discord"/></a>
- <img src="https://img.shields.io/badge/version-2.14.2--dev-blue?style=for-the-badge" alt="Version"/>
+ <img src="https://img.shields.io/badge/version-2.14.3--dev-blue?style=for-the-badge" alt="Version"/>
<img src="https://img.shields.io/badge/python-3.11+-green?style=for-the-badge&logo=python&logoColor=white" alt="Python"/>
<img src="https://img.shields.io/badge/license-PolyForm%20Small%20Business-blue?style=for-the-badge" alt="License"/>
</p>
diff --git a/pyproject.toml b/pyproject.toml
index 19107b8..942af3e 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[project]
name = "brainapi2"
-version = "2.14.2-dev"
+version = "2.14.3-dev"
description = "Version 2.x.x of the BrainAPI memory layer."
authors = [
{name = "Christian",email = "[email protected]"}
diff --git a/src/config.py b/src/config.py
index 85c3c86..38525d0 100644
--- a/src/config.py
+++ b/src/config.py
@@ -383,23 +383,6 @@ class OllamaConfig:
raise ValueError("Ollama configuration is not complete")
-class PricingConfig:
- """
- Configuration class for the Pricing configuration.
- """
-
- def __init__(self):
- """
- Initialize pricing configuration from environment variables.
-
- Attributes:
- input_token_price (float): Price per input token from INPUT_TOKEN_PRICE, defaults to 0.0 if unset.
- output_token_price (float): Price per output token from OUTPUT_TOKEN_PRICE, defaults to 0.0 if unset.
- """
- self.input_token_price = float(os.getenv("INPUT_TOKEN_PRICE", 0))
- self.output_token_price = float(os.getenv("OUTPUT_TOKEN_PRICE", 0))
-
-
class SpacyConfig:
"""
Configuration class for the Spacy configuration.
@@ -424,7 +407,7 @@ class Config:
"""
Initialize the application's central configuration by composing environment-backed sub-configurations and loading runtime flags.
- This constructor instantiates Azure, Redis, Neo4j, Milvus, Embeddings, Mongo, GCP, Celery, and Pricing configuration objects, reads the RUN_GRAPH_CONSOLIDATOR flag into `run_graph_consolidator`, and loads the `BRAINPAT_TOKEN` into `brainpat_token`.
+ This constructor instantiates Azure, Redis, Neo4j, Milvus, Embeddings, Mongo, GCP, and Celery configuration objects, reads the RUN_GRAPH_CONSOLIDATOR flag into `run_graph_consolidator`, and loads the `BRAINPAT_TOKEN` into `brainpat_token`.
Raises:
ValueError: If `BRAINPAT_TOKEN` is not set in the environment.
@@ -529,7 +512,6 @@ class Config:
self.milvus = MilvusConfig()
self.mongo = MongoConfig()
self.celery = CeleryConfig()
- self.pricing = PricingConfig()
self.spacy = SpacyConfig()
self.run_graph_consolidator = (
diff --git a/src/constants/agents.py b/src/constants/agents.py
index eab44a7..90b2f54 100644
--- a/src/constants/agents.py
+++ b/src/constants/agents.py
@@ -9,7 +9,7 @@ Modified By: Christian Nonis <[email protected]>
"""
from pydantic import BaseModel, Field
-from typing import Dict, Literal, Optional, List, Tuple
+from typing import Literal, Optional, List, Tuple
import uuid
@@ -106,8 +106,6 @@ class ArchitectAgentResponse(BaseModel):
new_nodes: List[ArchitectAgentNew]
relationships: List[ArchitectAgentRelationship]
- input_tokens: int
- output_tokens: int
class AtomicJanitorAgentWrongRelationship(BaseModel):
@@ -126,25 +124,3 @@ class AtomicJanitorAgentInputOutput(BaseModel):
class GraphConsolidatorOutput(BaseModel):
tasks: List[str] = Field(default_factory=list)
-
-
-class TokenInputDetail(BaseModel):
- total: int
- uncached: int
- cached: int
- cache_percentage: float
-
-
-class TokenOutputDetail(BaseModel):
- total: int
- regular: int
- reasoning: int
- reasoning_percentage: float
-
-
-class TokenDetail(BaseModel):
- input: TokenInputDetail
- output: TokenOutputDetail
- grand_total: int
- effective_total: int
- grouped_by_agent: Dict[str, "TokenDetail"] = Field(default_factory=dict)
diff --git a/src/core/agents/architect_agent.py b/src/core/agents/architect_agent.py
index 6abf376..dac8e60 100644
--- a/src/core/agents/architect_agent.py
+++ b/src/core/agents/architect_agent.py
@@ -68,7 +68,6 @@ from src.core.saving.ingestion_manager import IngestionManager
from src.services.api.constants.requests import IngestionTripleSet
from src.utils.cleanup import strip_properties
from src.utils.similarity.vectors import cosine_similarity
-from src.utils.tokens import merge_token_details, token_detail_from_token_counts
# from src.core.agents.tools.kg_agent import (
# KGAgentSearchGraphTool,
@@ -155,11 +154,6 @@ class ArchitectAgent:
self.vector_store = vector_store
self.embeddings = embeddings
self.agent = None
- self.token_detail = None
- self.input_tokens = 0
- self.output_tokens = 0
- self.cached_tokens = 0
- self.reasoning_tokens = 0
self.relationships_set: List[ArchitectAgentRelationship] = []
self.used_entities_dict = {}
self.ingestion_manager = ingestion_manager
@@ -343,8 +337,6 @@ class ArchitectAgent:
ArchitectAgentResponse: Contains:
- new_nodes: list of newly discovered nodes produced by the agent.
- relationships: list of relationships the agent created between entities or new nodes.
- - input_tokens: count of input tokens consumed during this run.
- - output_tokens: count of output tokens produced during this run.
"""
entities_dict = {entity.uuid: entity for entity in entities}
@@ -357,9 +349,6 @@ class ArchitectAgent:
type_="single",
)
- self.input_tokens = 0
- self.output_tokens = 0
-
def _invoke_agent(
ent: list[ScoutEntity],
all_rels: list[ArchitectAgentRelationship],
@@ -511,21 +500,6 @@ class ArchitectAgent:
previous_messages,
)
response = future.result(timeout=timeout)
- for m in response.get("messages", []):
- if hasattr(m, "usage_metadata"):
- self._update_token_counts(m.usage_metadata)
- self.token_detail = merge_token_details(
- [
- self.token_detail,
- token_detail_from_token_counts(
- self.input_tokens,
- self.output_tokens,
- self.cached_tokens,
- self.reasoning_tokens,
- "architect_agent",
- ),
- ]
- )
return response
except FutureTimeoutError:
raise TimeoutError(
@@ -588,8 +562,6 @@ class ArchitectAgent:
return ArchitectAgentResponse(
new_nodes=[],
relationships=[],
- input_tokens=self.input_tokens,
- output_tokens=self.output_tokens,
)
return ArchitectAgentResponse(
@@ -601,8 +573,6 @@ class ArchitectAgent:
_to_architect_relationship(relationship)
for relationship in all_relationships
],
- input_tokens=self.input_tokens,
- output_tokens=self.output_tokens,
)
def _persist_relationships(
@@ -658,11 +628,6 @@ class ArchitectAgent:
)
self.janitor_agent = janitor_agent
self._janitor_agent_brain_id = brain_id
- start_input_tokens = janitor_agent.input_tokens
- start_output_tokens = janitor_agent.output_tokens
- start_cached_tokens = janitor_agent.cached_tokens
- start_reasoning_tokens = janitor_agent.reasoning_tokens
-
janitor_response = janitor_agent.run_atomic_janitor(
input_relationships=input_rels,
text=text,
@@ -672,17 +637,6 @@ class ArchitectAgent:
max_retries=3,
)
- janitor_token_detail = token_detail_from_token_counts(
- janitor_agent.input_tokens - start_input_tokens,
- janitor_agent.output_tokens - start_output_tokens,
- janitor_agent.cached_tokens - start_cached_tokens,
- janitor_agent.reasoning_tokens - start_reasoning_tokens,
- "janitor_agent",
- )
- self.token_detail = merge_token_details(
- [self.token_detail, janitor_token_detail]
- )
-
required_new_nodes = getattr(janitor_response, "required_new_nodes", [])
if required_new_nodes:
for node in required_new_nodes:
@@ -870,8 +824,6 @@ class ArchitectAgent:
ArchitectAgentResponse: Contains:
- new_nodes: list of newly discovered nodes produced by the agent.
- relationships: list of relationships the agent created between entities or new nodes.
- - input_tokens: count of input tokens consumed during this run.
- - output_tokens: count of output tokens produced during this run.
"""
triple_entity_registry: Dict[Tuple[str, str], ArchitectAgentEntity] = {}
@@ -963,8 +915,6 @@ class ArchitectAgent:
type_="single",
)
- self.input_tokens = 0
- self.output_tokens = 0
def _invoke_agent(
ent: list[ScoutEntity],
@@ -1135,21 +1085,6 @@ class ArchitectAgent:
fix_content,
)
response = future.result(timeout=timeout)
- for m in response.get("messages", []):
- if hasattr(m, "usage_metadata"):
- self._update_token_counts(m.usage_metadata)
- self.token_detail = merge_token_details(
- [
- self.token_detail,
- token_detail_from_token_counts(
- self.input_tokens,
- self.output_tokens,
- self.cached_tokens,
- self.reasoning_tokens,
- "architect_agent",
- ),
- ]
- )
return response
except FutureTimeoutError:
raise TimeoutError(
@@ -1212,8 +1147,6 @@ class ArchitectAgent:
return ArchitectAgentResponse(
new_nodes=[],
relationships=[],
- input_tokens=self.input_tokens,
- output_tokens=self.output_tokens,
)
relationships_to_persist: List[ArchitectAgentRelationship] = []
@@ -1288,8 +1221,6 @@ class ArchitectAgent:
for new_node in all_new_nodes
],
relationships=relationships_to_persist,
- input_tokens=self.input_tokens,
- output_tokens=self.output_tokens,
)
def run_tooler(
@@ -1444,21 +1375,6 @@ class ArchitectAgent:
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(_invoke_agent, previous_messages)
response = future.result(timeout=timeout)
- for m in response.get("messages", []):
- if hasattr(m, "usage_metadata"):
- self._update_token_counts(m.usage_metadata)
- self.token_detail = merge_token_details(
- [
- self.token_detail,
- token_detail_from_token_counts(
- self.input_tokens,
- self.output_tokens,
- self.cached_tokens,
- self.reasoning_tokens,
- "architect_agent",
- ),
- ]
- )
return response
except FutureTimeoutError:
raise TimeoutError(
@@ -1506,25 +1422,3 @@ class ArchitectAgent:
return list(self.relationships_set)
- def _update_token_counts(self, usage_metadata: dict):
- """
- Update the agent's token counters from an LLM usage metadata dictionary.
-
- Parameters:
- usage_metadata (dict): Metadata containing token usage details. Expected keys:
- - "input_tokens": integer count of input tokens.
- - "output_tokens": integer count of output tokens.
- - "input_token_details": dict with "cache_read" (int) for cached input token reads.
- - "output_token_details": dict with "reasoning" (int) for tokens spent on reasoning.
- """
- # Base counts
- self.input_tokens += usage_metadata.get("input_tokens", 0)
- self.output_tokens += usage_metadata.get("output_tokens", 0)
-
- # Input details (caching)
- input_details = usage_metadata.get("input_token_details", {})
- self.cached_tokens += input_details.get("cache_read", 0)
-
- # Output details (reasoning)
- output_details = usage_metadata.get("output_token_details", {})
- self.reasoning_tokens += output_details.get("reasoning", 0)
diff --git a/src/core/agents/janitor_agent.py b/src/core/agents/janitor_agent.py
index abe57ab..226cdce 100644
--- a/src/core/agents/janitor_agent.py
+++ b/src/core/agents/janitor_agent.py
@@ -54,7 +54,6 @@ from src.core.agents.tools.janitor_agent.JanitorAgentSearchRelationshipTool impo
JanitorAgentSearchRelationshipsTool,
)
from src.utils.cleanup import strip_properties
-from src.utils.tokens import token_detail_from_token_counts
HISTORY_MAX_MESSAGES = 25
@@ -106,8 +105,6 @@ class JanitorAgent:
vector_store (VectorStoreAdapter): Adapter for vector search and retrieval.
embeddings (EmbeddingsAdapter): Adapter to compute embeddings for text.
database_desc (str): Human-readable description of the target database.
-
- Initializes internal token counters and token detail storage.
"""
self.llm_adapter = llm_adapter
self.kg = kg
@@ -115,11 +112,6 @@ class JanitorAgent:
self.embeddings = embeddings
self.agent = None
self.database_desc = database_desc
- self.input_tokens = 0
- self.output_tokens = 0
- self.cached_tokens = 0
- self.reasoning_tokens = 0
- self.token_detail = None
def _get_tools(self, brain_id: str = "default") -> List[BaseTool]:
"""
@@ -366,16 +358,6 @@ class JanitorAgent:
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(_invoke_agent, previous_messages)
response = future.result(timeout=timeout)
- for m in response.get("messages", []):
- if hasattr(m, "usage_metadata"):
- self._update_token_counts(m.usage_metadata)
- self.token_detail = token_detail_from_token_counts(
- self.input_tokens,
- self.output_tokens,
- self.cached_tokens,
- self.reasoning_tokens,
- "janitor_agent",
- )
return response
except FutureTimeoutError:
raise TimeoutError(
@@ -527,16 +509,6 @@ class JanitorAgent:
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(_invoke_agent, previous_messages)
response = future.result(timeout=timeout)
- for m in response.get("messages", []):
- if hasattr(m, "usage_metadata"):
- self._update_token_counts(m.usage_metadata)
- self.token_detail = token_detail_from_token_counts(
- self.input_tokens,
- self.output_tokens,
- self.cached_tokens,
- self.reasoning_tokens,
- "janitor_agent",
- )
return response
except FutureTimeoutError:
raise TimeoutError(
@@ -704,16 +676,6 @@ class JanitorAgent:
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(_invoke_agent, previous_messages)
response = future.result(timeout=timeout)
- for m in response.get("messages", []):
- if hasattr(m, "usage_metadata"):
- self._update_token_counts(m.usage_metadata)
- self.token_detail = token_detail_from_token_counts(
- self.input_tokens,
- self.output_tokens,
- self.cached_tokens,
- self.reasoning_tokens,
- "janitor_agent",
- )
return response
except FutureTimeoutError:
raise TimeoutError(
@@ -771,26 +733,3 @@ class JanitorAgent:
return "OK"
else:
return structured_response
-
- def _update_token_counts(self, usage_metadata: dict):
- """
- Update the agent's cumulative token counters from a message's usage metadata.
-
- Parameters:
- usage_metadata (dict): A mapping containing token usage information. Recognized keys:
- - "input_tokens": number to add to `input_tokens`
- - "output_tokens": number to add to `output_tokens`
- - "input_token_details": dict with optional "cache_read" to add to `cached_tokens`
- - "output_token_details": dict with optional "reasoning" to add to `reasoning_tokens`
- """
- # Base counts
- self.input_tokens += usage_metadata.get("input_tokens", 0)
- self.output_tokens += usage_metadata.get("output_tokens", 0)
-
- # Input details (caching)
- input_details = usage_metadata.get("input_token_details", {})
- self.cached_tokens += input_details.get("cache_read", 0)
-
- # Output details (reasoning)
- output_details = usage_metadata.get("output_token_details", {})
- self.reasoning_tokens += output_details.get("reasoning", 0)
diff --git a/src/core/agents/kg_agent.py b/src/core/agents/kg_agent.py
index 491571a..952b374 100644
--- a/src/core/agents/kg_agent.py
+++ b/src/core/agents/kg_agent.py
@@ -56,7 +56,6 @@ from src.core.agents.tools.kg_agent.KGAgentCreateRelationshipTool import (
KGAgentCreateRelationshipTool,
)
from src.core.agents.tools.kg_agent.KGAgentCreateNodeTool import KGAgentCreateNodeTool
-from src.utils.tokens import token_detail_from_token_counts
MAX_RECURSION_LIMIT = 50
MAX_RECURSION_LIMIT_GRAPH_CONSOLIDATOR = 50
@@ -99,8 +98,7 @@ class KGAgent:
database_desc (str): A brief description of the knowledge graph or database this agent will operate on.
Description:
- Stores provided adapters and clients on the instance, sets the agent to None, and initializes token accounting fields:
- `input_tokens`, `output_tokens`, `cached_tokens`, `reasoning_tokens` set to 0 and `token_detail` set to None.
+ Stores provided adapters and clients on the instance and sets the agent to None.
"""
self.llm_adapter = llm_adapter
self.cache_adapter = cache_adapter
@@ -109,11 +107,6 @@ class KGAgent:
self.embeddings = embeddings
self.agent = None
self.database_desc = database_desc
- self.input_tokens = 0
- self.output_tokens = 0
- self.cached_tokens = 0
- self.reasoning_tokens = 0
- self.token_detail = None
self._agent_type = None
self._agent_brain_id = None
@@ -582,9 +575,9 @@ class KGAgent:
)
def _invoke_agent_with_retry():
"""
- Invoke the graph-consolidator agent with a timeout and update token accounting from the response.
+ Invoke the graph-consolidator agent with a timeout.
- Waits up to `timeout` seconds for the agent invocation to complete. For any returned message that contains `usage_metadata`, updates the agent's token counters and `token_detail`. Returns the agent response dictionary.
+ Waits up to `timeout` seconds for the agent invocation to complete. Returns the agent response dictionary.
Returns:
dict: The agent response.
@@ -596,16 +589,6 @@ class KGAgent:
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(_invoke_agent)
response = future.result(timeout=timeout)
- for m in response.get("messages", []):
- if hasattr(m, "usage_metadata"):
- self._update_token_counts(m.usage_metadata)
- self.token_detail = token_detail_from_token_counts(
- self.input_tokens,
- self.output_tokens,
- self.cached_tokens,
- self.reasoning_tokens,
- "kg_agent",
- )
return response
except FutureTimeoutError:
raise TimeoutError(
@@ -630,30 +613,3 @@ class KGAgent:
raise
return "OK"
-
- def _update_token_counts(self, usage_metadata: dict):
- """
- Update the agent's token counters from LLM usage metadata.
-
- Reads token counts from the provided `usage_metadata` mapping and increments the agent's
- internal counters: `input_tokens`, `output_tokens`, `cached_tokens`, and `reasoning_tokens`.
- Missing entries are treated as zero.
-
- Parameters:
- usage_metadata (dict): Usage metadata containing any of the following keys:
- - "input_tokens": total input token count (int)
- - "output_tokens": total output token count (int)
- - "input_token_details": dict with "cache_read" (int) for cached input tokens
- - "output_token_details": dict with "reasoning" (int) for reasoning/output tokens
- """
- # Base counts
- self.input_tokens += usage_metadata.get("input_tokens", 0)
- self.output_tokens += usage_metadata.get("output_tokens", 0)
-
- # Input details (caching)
- input_details = usage_metadata.get("input_token_details", {})
- self.cached_tokens += input_details.get("cache_read", 0)
-
- # Output details (reasoning)
- output_details = usage_metadata.get("output_token_details", {})
- self.reasoning_tokens += output_details.get("reasoning", 0)
diff --git a/src/core/agents/scout_agent.py b/src/core/agents/scout_agent.py
index 9a7264c..100bdd4 100644
--- a/src/core/agents/scout_agent.py
+++ b/src/core/agents/scout_agent.py
@@ -40,7 +40,6 @@ from src.constants.prompts.scout_agent import (
from src.core.agents.core import parse_structured_from_messages, runtime_agent_factory
from src.core.plugins.prompts import prompt_registry
from src.services.api.constants.requests import IngestionTripleSet
-from src.utils.tokens import token_detail_from_token_counts
class _ScoutEntity(BaseModel):
@@ -81,8 +80,6 @@ class ScoutAgentResponse(BaseModel):
"""
entities: List[ScoutEntity]
- input_tokens: int
- output_tokens: int
class ScoutAgent:
@@ -99,9 +96,9 @@ class ScoutAgent:
embeddings: EmbeddingsAdapter,
):
"""
- Initialize a ScoutAgent with the provided adapters and reset internal agent and token-tracking state.
+ Initialize a ScoutAgent with the provided adapters and reset internal agent state.
- Stores the provided LLM, cache, knowledge graph, vector store, and embeddings adapters on the instance, sets the agent and token_detail to None, and initializes input_tokens, output_tokens, cached_tokens, and reasoning_tokens counters to zero.
+ Stores the provided LLM, cache, knowledge graph, vector store, and embeddings adapters on the instance and sets the agent to None.
"""
self.llm_adapter = llm_adapter
self.cache_adapter = cache_adapter
@@ -109,11 +106,6 @@ class ScoutAgent:
self.vector_store = vector_store
self.embeddings = embeddings
self.agent = None
- self.token_detail = None
- self.input_tokens = 0
- self.output_tokens = 0
- self.cached_tokens = 0
- self.reasoning_tokens = 0
def _get_tools(self, brain_id: str = "default") -> List[BaseTool]:
"""
@@ -173,7 +165,7 @@ class ScoutAgent:
"""
Given the provided text and partial/full triples, extract all the (missing) entities from the text.
- Performs an LLM invocation (with optional targeting context), applies retries with exponential backoff on timeouts, enforces a per-invocation timeout, and accumulates token usage from the agent responses.
+ Performs an LLM invocation (with optional targeting context), applies retries with exponential backoff on timeouts, and enforces a per-invocation timeout.
Parameters:
text: The input text to extract entities from.
@@ -188,8 +180,6 @@ class ScoutAgent:
Returns:
A ScoutAgentResponse containing:
- entities: list of extracted ScoutEntity objects.
- - input_tokens: accumulated input token count observed during the run.
- - output_tokens: accumulated output token count observed during the run.
Raises:
TimeoutError: If a single invocation exceeds `timeout`, or if all retry attempts fail due to timeouts.
@@ -284,7 +274,7 @@ class ScoutAgent:
)
def _invoke_agent_with_retry():
"""
- Invoke the agent in a separate thread, enforce the configured timeout, and update token accounting from the agent's response.
+ Invoke the agent in a separate thread and enforce the configured timeout.
Returns:
dict: The agent response dictionary.
@@ -296,16 +286,6 @@ class ScoutAgent:
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(_invoke_agent)
response = future.result(timeout=timeout)
- for m in response.get("messages", []):
- if hasattr(m, "usage_metadata"):
- self._update_token_counts(m.usage_metadata)
- self.token_detail = token_detail_from_token_counts(
- self.input_tokens,
- self.output_tokens,
- self.cached_tokens,
- self.reasoning_tokens,
- "scout_agent",
- )
return response
except FutureTimeoutError:
raise TimeoutError(
@@ -343,8 +323,6 @@ class ScoutAgent:
ScoutEntity(**entity.model_dump(mode="json"))
for entity in structured.entities
],
- input_tokens=self.input_tokens,
- output_tokens=self.output_tokens,
)
def run(
@@ -358,9 +336,9 @@ class ScoutAgent:
mode: Literal["granular", "coarse"] = "granular",
) -> ScoutAgentResponse:
"""
- Extract entities from the provided text using the Scout agent and return a structured response containing the entities and token usage.
+ Extract entities from the provided text using the Scout agent and return a structured response containing the entities.
- Performs an LLM invocation (with optional targeting context), applies retries with exponential backoff on timeouts, enforces a per-invocation timeout, and accumulates token usage from the agent responses.
+ Performs an LLM invocation (with optional targeting context), applies retries with exponential backoff on timeouts, and enforces a per-invocation timeout.
Parameters:
text: The input text to extract entities from.
@@ -373,8 +351,6 @@ class ScoutAgent:
Returns:
A ScoutAgentResponse containing:
- entities: list of extracted ScoutEntity objects.
- - input_tokens: accumulated input token count observed during the run.
- - output_tokens: accumulated output token count observed during the run.
Raises:
TimeoutError: If a single invocation exceeds `timeout`, or if all retry attempts fail due to timeouts.
@@ -445,7 +421,7 @@ class ScoutAgent:
)
def _invoke_agent_with_retry():
"""
- Invoke the agent in a separate thread, enforce the configured timeout, and update token accounting from the agent's response.
+ Invoke the agent in a separate thread and enforce the configured timeout.
Returns:
dict: The agent response dictionary.
@@ -457,16 +433,6 @@ class ScoutAgent:
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(_invoke_agent)
response = future.result(timeout=timeout)
- for m in response.get("messages", []):
- if hasattr(m, "usage_metadata"):
- self._update_token_counts(m.usage_metadata)
- self.token_detail = token_detail_from_token_counts(
- self.input_tokens,
- self.output_tokens,
- self.cached_tokens,
- self.reasoning_tokens,
- "scout_agent",
- )
return response
except FutureTimeoutError:
raise TimeoutError(
@@ -504,29 +470,4 @@ class ScoutAgent:
ScoutEntity(**entity.model_dump(mode="json"))
for entity in structured.entities
],
- input_tokens=self.input_tokens,
- output_tokens=self.output_tokens,
)
-
- def _update_token_counts(self, usage_metadata: dict):
- """
- Update the agent's accumulated token counters from a usage metadata dictionary.
-
- Parameters:
- usage_metadata (dict): Metadata containing token counts. Expected keys:
- - "input_tokens": integer count to add to input_tokens (defaults to 0)
- - "output_tokens": integer count to add to output_tokens (defaults to 0)
- - "input_token_details": dict with optional "cache_read" integer to add to cached_tokens (defaults to 0)
- - "output_token_details": dict with optional "reasoning" integer to add to reasoning_tokens (defaults to 0)
- """
- # Base counts
- self.input_tokens += usage_metadata.get("input_tokens", 0)
- self.output_tokens += usage_metadata.get("output_tokens", 0)
-
- # Input details (caching)
- input_details = usage_metadata.get("input_token_details", {})
- self.cached_tokens += input_details.get("cache_read", 0)
-
- # Output details (reasoning)
- output_details = usage_metadata.get("output_token_details", {})
- self.reasoning_tokens += output_details.get("reasoning", 0)
diff --git a/src/core/agents/tools/architect_agent/ArchitectAgentCreateRelationshipTool.py b/src/core/agents/tools/architect_agent/ArchitectAgentCreateRelationshipTool.py
index 463c92d..1bf23dc 100644
--- a/src/core/agents/tools/architect_agent/ArchitectAgentCreateRelationshipTool.py
+++ b/src/core/agents/tools/architect_agent/ArchitectAgentCreateRelationshipTool.py
@@ -35,7 +35,6 @@ from src.utils.cleanup import strip_properties
from src.utils.nlp.names import levenshtein_similarity
from src.utils.serialization.data import is_uuid
from src.utils.similarity.vectors import cosine_similarity
-from src.utils.tokens import merge_token_details, token_detail_from_token_counts
class ArchitectAgentCreateRelationshipTool(BaseTool):
@@ -399,11 +398,6 @@ class ArchitectAgentCreateRelationshipTool(BaseTool):
)
self.architect_agent.janitor_agent = janitor_agent
self.architect_agent._janitor_agent_brain_id = self.brain_id
- start_input_tokens = janitor_agent.input_tokens
- start_output_tokens = janitor_agent.output_tokens
- start_cached_tokens = janitor_agent.cached_tokens
- start_reasoning_tokens = janitor_agent.reasoning_tokens
-
janitor_response: AtomicJanitorAgentInputOutput = (
janitor_agent.run_atomic_janitor(
input_relationships=input_rels,
@@ -420,17 +414,6 @@ class ArchitectAgentCreateRelationshipTool(BaseTool):
janitor_response,
)
- janitor_token_detail = token_detail_from_token_counts(
- janitor_agent.input_tokens - start_input_tokens,
- janitor_agent.output_tokens - start_output_tokens,
- janitor_agent.cached_tokens - start_cached_tokens,
- janitor_agent.reasoning_tokens - start_reasoning_tokens,
- "janitor_agent",
- )
- self.architect_agent.token_detail = merge_token_details(
- [self.architect_agent.token_detail, janitor_token_detail]
- )
-
required_new_nodes = getattr(janitor_response, "required_new_nodes", [])
if required_new_nodes:
diff --git a/src/core/layers/graph_consolidation/graph_consolidation.py b/src/core/layers/graph_consolidation/graph_consolidation.py
index 3f4d721..6dbac3a 100644
--- a/src/core/layers/graph_consolidation/graph_consolidation.py
+++ b/src/core/layers/graph_consolidation/graph_consolidation.py
@@ -9,36 +9,13 @@ Modified By: Christian Nonis <[email protected]>
-----
"""
-import json
from typing import List
-from concurrent.futures import ThreadPoolExecutor, TimeoutError as FutureTimeoutError
-from tenacity import (
- retry,
- stop_after_attempt,
- wait_exponential,
- retry_if_exception_type,
- RetryError,
-)
-from pydantic import BaseModel
-from langchain_core.messages import HumanMessage, RemoveMessage
-from src.config import config
-from src.constants.agents import (
- ArchitectAgentEntity,
- GraphConsolidatorOutput,
- TokenDetail,
-)
-from src.constants.kg import Node, Predicate
from src.core.agents.architect_agent import ArchitectAgentRelationship
from src.core.agents.janitor_agent import (
JanitorAgent,
- HISTORY_MAX_MESSAGES,
- HISTORY_MAX_MESSAGES_DELETE,
)
from src.core.agents.kg_agent import KGAgent
-from src.core.agents.tools.janitor_agent import (
- JanitorAgentExecuteGraphReadOperationTool,
-)
from src.services.input.agents import (
cache_adapter,
embeddings_adapter,
@@ -48,33 +25,21 @@ from src.services.input.agents import (
)
from src.core.instances import llm_small_adapter as llm_adapter
from src.lib.neo4j.client import _neo4j_client
-from src.utils.tokens import merge_token_details, token_detail_from_token_counts
RELATIONSHIP_BATCH_SIZE = 20
-class ConsolidationResponse(BaseModel):
- """
- Response for the graph consolidation.
- """
-
- token_detail: TokenDetail
-
-
def consolidate_graph(
new_relationships: List[ArchitectAgentRelationship],
brain_id: str = "default",
-) -> ConsolidationResponse:
+) -> None:
"""
Consolidates and normalizes a collection of new knowledge-graph relationships across the graph.
- Processes the provided relationships in batches to perform macroscopic fixes such as name normalization, connection normalization, and deduplication across multiple relationships and graph areas. Collects and merges token usage metrics produced during consolidation.
+ Processes the provided relationships in batches to perform macroscopic fixes such as name normalization, connection normalization, and deduplication across multiple relationships and graph areas.
Parameters:
brain_id (str): Identifier of the target knowledge graph/brain to consolidate into (defaults to "default").
-
- Returns:
- ConsolidationResponse: Response containing merged token usage details for the consolidation run.
"""
batches = [
@@ -87,9 +52,6 @@ def consolidate_graph(
len(batches),
)
- token_details = []
- token_detail = None
-
for batch in batches:
janitor_agent = JanitorAgent(
@@ -112,19 +74,6 @@ def consolidate_graph(
tasks,
)
- token_details.append(
- token_detail_from_token_counts(
- janitor_agent.input_tokens,
- janitor_agent.output_tokens,
- janitor_agent.cached_tokens,
- janitor_agent.reasoning_tokens,
- "janitor_agent",
- )
- )
-
- # 2. ~~Something to review and approve/reject what janitor proposed~~
- # Another agent that reviews the porposed fixes and approves or rejects them .. what ?
-
kg_agent = KGAgent(
llm_adapter=llm_small_adapter,
cache_adapter=cache_adapter,
@@ -144,27 +93,3 @@ def consolidate_graph(
)
print(e)
continue
- token_details.append(
- token_detail_from_token_counts(
- kg_agent.input_tokens,
- kg_agent.output_tokens,
- kg_agent.cached_tokens,
- kg_agent.reasoning_tokens,
- "kg_agent",
- )
- )
-
- token_detail = merge_token_details(token_details)
-
- print("================================================")
- print(
- f"> Input tokens: {token_detail.input.total} -> ${token_detail.input.total * config.pricing.input_token_price}"
- )
- print(
- f"> Output tokens: {token_detail.output.total} -> ${token_detail.output.total * config.pricing.output_token_price}"
- )
- print("================================================")
-
- return ConsolidationResponse(
- token_detail=token_detail,
- )
diff --git a/src/core/saving/auto_kg.py b/src/core/saving/auto_kg.py
index f7eef30..307a5fb 100644
--- a/src/core/saving/auto_kg.py
+++ b/src/core/saving/auto_kg.py
@@ -16,7 +16,6 @@ from src.config import config
from src.constants.kg import Node
from src.core.agents.scout_agent import ScoutAgent
from src.core.agents.architect_agent import ArchitectAgent
-from src.core.layers.graph_consolidation.graph_consolidation import consolidate_graph
from src.core.saving.ingestion_manager import IngestionManager
from src.services.input.agents import (
cache_adapter,
@@ -25,7 +24,6 @@ from src.services.input.agents import (
llm_small_adapter,
vector_store_adapter,
)
-from src.utils.tokens import merge_token_details, token_detail_from_token_counts
import langsmith
@@ -36,7 +34,7 @@ def enrich_kg_from_input(
"""
Orchestrates enrichment of the knowledge graph from a free-text input.
- Runs the scout and architect agents to extract entities and relationships from the provided input, optionally consolidates the resulting relationships into the graph, and reports token usage and cost metrics. Side effects include updating the knowledge graph and printing debug and cost summaries to standard output.
+ Runs the scout and architect agents to extract entities and relationships from the provided input, and optionally consolidates the resulting relationships into the graph. Side effects include updating the knowledge graph and printing debug summaries to standard output.
Parameters:
input (str): Free-text content to process and ingest into the knowledge graph.
@@ -105,7 +103,6 @@ def _enrich_kg_impl(input: str, targeting, brain_id: str, ingestion_session_id:
return
if config.pipeline_mode == "accurate":
- token_details = []
print(f"[DEBUG (ingestion_session_id)]: {ingestion_session_id}")
entities = scout_agent.run(
@@ -114,15 +111,6 @@ def _enrich_kg_impl(input: str, targeting, brain_id: str, ingestion_session_id:
brain_id=brain_id,
ingestion_session_id=ingestion_session_id,
)
- token_details.append(
- token_detail_from_token_counts(
- scout_agent.input_tokens,
- scout_agent.output_tokens,
- scout_agent.cached_tokens,
- scout_agent.reasoning_tokens,
- "scout_agent",
- )
- )
architect_agent.run_tooler(
input,
@@ -132,15 +120,6 @@ def _enrich_kg_impl(input: str, targeting, brain_id: str, ingestion_session_id:
timeout=20000,
ingestion_session_id=ingestion_session_id,
)
- token_details.append(
- token_detail_from_token_counts(
- architect_agent.input_tokens,
- architect_agent.output_tokens,
- architect_agent.cached_tokens,
- architect_agent.reasoning_tokens,
- "architect_agent",
- )
- )
if config.run_graph_consolidator and architect_agent.session_id:
from src.lib.redis.client import _redis_client
@@ -192,29 +171,6 @@ def _enrich_kg_impl(input: str, targeting, brain_id: str, ingestion_session_id:
f"[DEBUG (enrich_kg_from_input)]: Consolidation task {task_result.id} queued"
)
- token_details = merge_token_details(
- [
- scout_agent.token_detail,
- architect_agent.token_detail,
- ]
- )
-
- print("-----------------------------------")
- print(
- f"> Input tokens: {token_details.input.total} -> ${token_details.input.total * config.pricing.input_token_price}"
- )
- print(
- f"> Output tokens: {token_details.output.total} -> ${token_details.output.total * config.pricing.output_token_price}"
- )
- print(
- f"> Cost -> ${token_details.input.total * config.pricing.input_token_price + token_details.output.total * config.pricing.output_token_price} for {len(input)} characters"
- )
- print(
- f"> Cost/Character -> ${((token_details.input.total * config.pricing.input_token_price + token_details.output.total * config.pricing.output_token_price) / len(input)):.12f}"
- )
- print("> Token details: ", token_details.model_dump_json(indent=2))
- print("-----------------------------------")
-
try:
from langchain_core.tracers.langchain import wait_for_all_tracers
diff --git a/src/utils/tokens.py b/src/utils/tokens.py
deleted file mode 100644
index 21e27f4..0000000
--- a/src/utils/tokens.py
+++ /dev/null
@@ -1,142 +0,0 @@
-"""
-File: /tokens.py
-Project: utils
-Created Date: Saturday January 17th 2026
-Author: Christian Nonis <[email protected]>
------
-Last Modified: Saturday January 17th 2026 11:22:10 pm
-Modified By: Christian Nonis <[email protected]>
------
-"""
-
-from src.constants.agents import TokenDetail, TokenInputDetail, TokenOutputDetail
-
-
-def _build_token_detail(
- input_tokens: int,
- output_tokens: int,
- cached_tokens: int,
- reasoning_tokens: int,
- grouped_by_agent: dict[str, TokenDetail] | None = None,
-) -> TokenDetail:
- if grouped_by_agent is None:
- grouped_by_agent = {}
- return TokenDetail(
- input=TokenInputDetail(
- total=input_tokens,
- uncached=input_tokens - cached_tokens,
- cached=cached_tokens,
- cache_percentage=(
- (cached_tokens / input_tokens * 100) if input_tokens > 0 else 0.0
- ),
- ),
- output=TokenOutputDetail(
- total=output_tokens,
- regular=output_tokens - reasoning_tokens,
- reasoning=reasoning_tokens,
- reasoning_percentage=(
- (reasoning_tokens / output_tokens * 100) if output_tokens > 0 else 0.0
- ),
- ),
- grand_total=input_tokens + output_tokens,
- effective_total=input_tokens - cached_tokens + output_tokens,
- grouped_by_agent=grouped_by_agent,
- )
-
-
-def merge_token_details(token_details: list[TokenDetail]) -> TokenDetail:
- """
- Aggregate a list of TokenDetail objects into a single merged TokenDetail.
-
- Parameters:
- token_details (list[TokenDetail]): List of TokenDetail objects to merge. None entries are ignored; if the list is empty or contains only None, a TokenDetail with all zero/default fields is returned.
-
- Returns:
- TokenDetail: A merged TokenDetail where input and output counts are summed across entries, cache_percentage is (total cached / total input * 100) or 0.0 if total input is zero, reasoning_percentage is (total reasoning / total output * 100) or 0.0 if total output is zero, and grand_total and effective_total are the sums of their respective values.
- """
- if not token_details:
- return _build_token_detail(0, 0, 0, 0)
-
- token_details = [detail for detail in token_details if detail is not None]
- if not token_details:
- return _build_token_detail(0, 0, 0, 0)
-
- total_input = sum(detail.input.total for detail in token_details)
- total_uncached = sum(detail.input.uncached for detail in token_details)
- total_cached = sum(detail.input.cached for detail in token_details)
- cache_percentage = (total_cached / total_input * 100) if total_input > 0 else 0.0
-
- total_output = sum(detail.output.total for detail in token_details)
- total_regular = sum(detail.output.regular for detail in token_details)
- total_reasoning = sum(detail.output.reasoning for detail in token_details)
- reasoning_percentage = (
- (total_reasoning / total_output * 100) if total_output > 0 else 0.0
- )
-
- grand_total = sum(detail.grand_total for detail in token_details)
- effective_total = sum(detail.effective_total for detail in token_details)
-
- grouped_by_agent: dict[str, TokenDetail] = {}
- for detail in token_details:
- for agent_name, agent_detail in detail.grouped_by_agent.items():
- if agent_name in grouped_by_agent:
- grouped_by_agent[agent_name] = merge_token_details(
- [grouped_by_agent[agent_name], agent_detail]
- )
- else:
- grouped_by_agent[agent_name] = agent_detail
-
- return TokenDetail(
- input=TokenInputDetail(
- total=total_input,
- uncached=total_uncached,
- cached=total_cached,
- cache_percentage=cache_percentage,
- ),
- output=TokenOutputDetail(
- total=total_output,
- regular=total_regular,
- reasoning=total_reasoning,
- reasoning_percentage=reasoning_percentage,
- ),
- grand_total=grand_total,
- effective_total=effective_total,
- grouped_by_agent=grouped_by_agent,
- )
-
-
-def token_detail_from_token_counts(
- input_tokens: int,
- output_tokens: int,
- cached_tokens: int,
- reasoning_tokens: int,
- agent_name: str | None = None,
-) -> TokenDetail:
- """
- Constructs a TokenDetail summarizing input and output token counts with cached and reasoning breakdowns.
-
- Parameters:
- input_tokens (int): Total number of input tokens.
- output_tokens (int): Total number of output tokens.
- cached_tokens (int): Number of input tokens served from cache.
- reasoning_tokens (int): Number of output tokens classified as reasoning.
-
- Returns:
- TokenDetail: Aggregated token details containing:
- - input: TokenInputDetail with total, uncached, cached, and cache_percentage.
- - output: TokenOutputDetail with total, regular, reasoning, and reasoning_percentage.
- - grand_total: Sum of input and output tokens.
- - effective_total: Sum of output tokens plus input tokens not served from cache.
- """
- base_detail = _build_token_detail(
- input_tokens, output_tokens, cached_tokens, reasoning_tokens
- )
- if agent_name:
- return _build_token_detail(
- input_tokens,
- output_tokens,
- cached_tokens,
- reasoning_tokens,
- grouped_by_agent={agent_name: base_detail},
- )
- return base_detail
diff --git a/src/workers/tasks/ingestion.py b/src/workers/tasks/ingestion.py
index 81d2879..a357e92 100644
--- a/src/workers/tasks/ingestion.py
+++ b/src/workers/tasks/ingestion.py
@@ -951,7 +951,7 @@ def consolidate_graph_async(
tags=["consolidate_graph", "janitor", "kg_agent"],
metadata=tracing_metadata,
):
- consolidation_response = consolidate_graph(relationships, brain_id=brain_id)
+ consolidate_graph(relationships, brain_id=brain_id)
try:
from langchain_core.tracers.langchain import wait_for_all_tracers