Amortize LLM research cost across generated articles (v0.5.2)

Research LLM call runs once per pipeline but was logged with no run_id
or post_id, making it invisible in per-article cost breakdowns. Now the
pipeline's cost tracker flows through Source_Collector to the research
provider, tagging costs with the run_id. After all articles complete,
amortize_research_costs() divides the cost evenly and inserts one row
per article. link_generation_logs() excludes llm_research stage so the
first article doesn't grab the full cost.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
peptiderepo
2026-04-20 00:44:33 +07:00
parent 327cdc08d9
commit 264fcf0f84
6 changed files with 153 additions and 7 deletions

View File

@@ -20,6 +20,24 @@ declare(strict_types=1);
*/
class PRAutoBlogger_Source_Collector {
/**
* Pipeline cost tracker — passed to providers that need run_id tagging.
* Null when called outside the pipeline (e.g., manual test collection).
*/
private ?PRAutoBlogger_Cost_Tracker $cost_tracker = null;
/**
* Inject the pipeline's cost tracker so source providers can log costs
* with the correct run_id. Without this, provider costs are orphaned.
*
* @param PRAutoBlogger_Cost_Tracker $cost_tracker Pipeline cost tracker with run_id set.
* @return $this
*/
public function set_cost_tracker( PRAutoBlogger_Cost_Tracker $cost_tracker ): self {
$this->cost_tracker = $cost_tracker;
return $this;
}
/**
* Collect data from all enabled source providers and store in database.
*
@@ -219,10 +237,16 @@ class PRAutoBlogger_Source_Collector {
}
if ( 'llm_research' === $source_type ) {
return [
$config = [
'model' => get_option( 'prautoblogger_research_model', PRAUTOBLOGGER_DEFAULT_ANALYSIS_MODEL ),
'prompt' => get_option( 'prautoblogger_research_prompt', '' ),
];
// Pass the pipeline's cost tracker so research costs get tagged
// with the run_id for post-pipeline amortization across articles.
if ( null !== $this->cost_tracker ) {
$config['cost_tracker'] = $this->cost_tracker;
}
return $config;
}
return [];