Files
wehub-resource-sync c889a57b6b
Test Suites / Build CI Environment (push) Has been cancelled
Test Suites / Basic Tests (push) Has been cancelled
Test Suites / End-to-End Tests (push) Has been cancelled
Test Suites / CLI Tests (push) Has been cancelled
Test Suites / Slow End-to-End Tests (push) Has been cancelled
Test Suites / Graph Database Tests (push) Has been cancelled
Test Suites / Vector DB Tests (push) Has been cancelled
Test Suites / Temporal Graph Test (push) Has been cancelled
Test Suites / Search Test on Different DBs (push) Has been cancelled
Test Suites / Example Tests (push) Has been cancelled
Test Suites / Notebook Tests (push) Has been cancelled
Test Suites / OS and Python Tests Ubuntu (push) Has been cancelled
Test Suites / OS and Python Tests Extended (push) Has been cancelled
Test Suites / LLM Test Suite (push) Has been cancelled
Test Suites / S3 File Storage Test (push) Has been cancelled
Test Suites / Run Integration Tests (push) Has been cancelled
Test Suites / MCP Tests (push) Has been cancelled
Test Suites / Docker Compose Test (push) Has been cancelled
Test Suites / Docker CI test (push) Has been cancelled
Test Suites / Relational DB Migration Tests (push) Has been cancelled
Test Suites / Distributed Cognee Test (push) Has been cancelled
Test Suites / DB Examples Tests (push) Has been cancelled
Test Suites / Test Completion Status (push) Has been cancelled
Test Suites / Claude Code Review (push) Has been cancelled
Test Suites / basic checks (push) Has been cancelled
build | Build and Push Cognee MCP Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
build | Build and Push Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.11) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.12) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (kuzu, kuzu) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (neo4j, neo4j) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Examples (push) Has been cancelled
Weighted Edges Tests / Code Quality for Weighted Edges (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:02:24 +08:00

115 lines
4.0 KiB
Python

"""Cost comparison between full-context prompting and cognee persistent memory.
Pure arithmetic, no IO. The two querying strategies are modelled as objects that
each compute their own cumulative token cost over a number of queries. Every other
figure (parity, reduction milestones) is derived from those two objects.
"""
from __future__ import annotations
from dataclasses import dataclass
@dataclass(frozen=True)
class ChunkMeasurement:
"""One sampled chunk run through one llm_model, in real measured tokens.
Prompt/completion counts come from the LLM response, so the instruction
wrapper and Pydantic schema are already included. input_tokens is a local
token count of the chunk content, sharing a basis with corpus_tokens.
"""
llm_model: str
input_tokens: int
summary_prompt_tokens: int
summary_completion_tokens: int
graph_prompt_tokens: int
graph_completion_tokens: int
@property
def ingestion_tokens(self) -> int:
return (
self.summary_prompt_tokens
+ self.summary_completion_tokens
+ self.graph_prompt_tokens
+ self.graph_completion_tokens
)
@property
def graph_ratio(self) -> float:
"""Graph-extraction output per content token — the density signal."""
return self.graph_completion_tokens / self.input_tokens
@property
def summary_ratio(self) -> float:
return self.summary_completion_tokens / self.input_tokens
@dataclass(frozen=True)
class FullContextQueryCost:
"""Cost of answering queries by sending the whole corpus each time."""
corpus_tokens: int
query_overhead_tokens: int # instruction wrapper + question
def tokens(self, queries: int) -> float:
return queries * (self.corpus_tokens + self.query_overhead_tokens)
@dataclass(frozen=True)
class CogneeQueryCost:
"""Cost of answering queries via cognee: ingest once, then retrieve per query."""
ingestion_tokens: float # one-time cognee.remember() over the whole corpus
retrieved_context_tokens: int # cognee.recall() context per query (~constant at scale)
def tokens(self, queries: int) -> float:
return self.ingestion_tokens + queries * self.retrieved_context_tokens
def average_measurement(chunk_measurements: list[ChunkMeasurement]) -> ChunkMeasurement:
"""Return the typical chunk for one llm_model: the mean of the token fields.
The ratios are properties, so they derive correctly from these means.
"""
count = len(chunk_measurements)
def mean(field: str) -> int:
return round(sum(getattr(m, field) for m in chunk_measurements) / count)
return ChunkMeasurement(
llm_model=chunk_measurements[0].llm_model,
input_tokens=mean("input_tokens"),
summary_prompt_tokens=mean("summary_prompt_tokens"),
summary_completion_tokens=mean("summary_completion_tokens"),
graph_prompt_tokens=mean("graph_prompt_tokens"),
graph_completion_tokens=mean("graph_completion_tokens"),
)
def corpus_ingestion_tokens(average: ChunkMeasurement, corpus_tokens: int) -> float:
"""Scale one chunk's measured ingestion cost up to the whole corpus.
The multiplier is ingestion tokens per content token; both the chunk's
input_tokens and corpus_tokens are local counts, so they share a basis.
"""
multiplier = average.ingestion_tokens / average.input_tokens
return multiplier * corpus_tokens
def queries_for_reduction(
full_context_cost: FullContextQueryCost,
cognee_cost: CogneeQueryCost,
factor: float,
) -> float | None:
"""Queries at which full-context costs `factor`x as much as cognee.
factor 1.0 is parity (the cross-over). Returns None when the factor is
unreachable, i.e. cognee's per-query cost alone already exceeds the target.
"""
full_per_query = full_context_cost.corpus_tokens + full_context_cost.query_overhead_tokens
denominator = full_per_query - factor * cognee_cost.retrieved_context_tokens
if denominator <= 0:
return None
return factor * cognee_cost.ingestion_tokens / denominator