Cost control

This commit is contained in:
2026-01-27 09:25:11 +01:00
parent 37eb03583c
commit 0ed89a7045
9 changed files with 733 additions and 6 deletions

View File

@@ -22,7 +22,12 @@ class EmailGenerator:
self.env = Environment(loader=FileSystemLoader(template_dir))
def generate_digest_email(
self, entries: list[DigestEntry], date_str: str, subject: str
self,
entries: list[DigestEntry],
date_str: str,
subject: str,
session_cost: float = 0.0,
cumulative_cost: float = 0.0,
) -> tuple[str, str]:
"""
Generate HTML email for daily digest
@@ -31,6 +36,8 @@ class EmailGenerator:
entries: List of digest entries (articles with summaries)
date_str: Date string for the digest
subject: Email subject line
session_cost: Cost for this digest generation
cumulative_cost: Total cost across all runs
Returns:
Tuple of (html_content, text_content)
@@ -54,6 +61,8 @@ class EmailGenerator:
"total_sources": unique_sources,
"total_categories": len(sorted_categories),
"articles_by_category": {cat: articles_by_category[cat] for cat in sorted_categories},
"session_cost": session_cost,
"cumulative_cost": cumulative_cost,
}
# Render HTML template
@@ -64,14 +73,21 @@ class EmailGenerator:
html_inlined = transform(html)
# Generate plain text version
text = self._generate_text_version(entries, date_str, subject)
text = self._generate_text_version(
entries, date_str, subject, session_cost, cumulative_cost
)
logger.debug(f"Generated email with {len(entries)} articles")
return html_inlined, text
def _generate_text_version(
self, entries: list[DigestEntry], date_str: str, subject: str
self,
entries: list[DigestEntry],
date_str: str,
subject: str,
session_cost: float = 0.0,
cumulative_cost: float = 0.0,
) -> str:
"""Generate plain text version of email"""
lines = [
@@ -110,5 +126,9 @@ class EmailGenerator:
lines.append("")
lines.append("---")
lines.append("Generated by News Agent | Powered by OpenRouter AI")
lines.append("")
lines.append("COST INFORMATION")
lines.append(f"This digest: ${session_cost:.4f}")
lines.append(f"Total spent: ${cumulative_cost:.4f}")
return "\n".join(lines)

View File

@@ -188,6 +188,15 @@
<div class="footer">
<p>Generated by News Agent | Powered by OpenRouter AI</p>
<p>You received this because you subscribed to daily tech news digests</p>
<hr style="margin: 20px 0; border: none; border-top: 1px solid #e5e7eb;">
<div style="background-color: #f9fafb; padding: 15px; border-radius: 6px; margin-top: 20px;">
<p style="margin: 0; font-weight: 600; color: #374151;">💰 Cost Information</p>
<p style="margin: 5px 0 0 0; font-size: 14px;">
<span style="color: #059669;">This digest: ${{ "%.4f"|format(session_cost) }}</span>
&nbsp;|&nbsp;
<span style="color: #2563eb;">Total spent: ${{ "%.4f"|format(cumulative_cost) }}</span>
</p>
</div>
</div>
</div>
</body>