refactor(meet-kevin): switch LLM analyzer to OpenRouter via OpenAI SDK

User's Vault has openrouter_api_key but no direct sk-ant-* Anthropic key.
OpenRouter passes through Claude Sonnet 4.6 (~3% markup over Anthropic
list pricing) and matches the existing gpt_mini_endpoint pattern used
by recruiter-responder.

- Replace anthropic.AsyncAnthropic with openai.AsyncOpenAI + base_url
- Convert Anthropic tool-use API to OpenAI function-calling
- System prompt unchanged (analyst instructions are model-agnostic)
- Drop cache_control (not in OpenAI API); revisit later if cost matters
- Model slug: anthropic/claude-sonnet-4.5 (OpenRouter's current Claude tier)
- Pricing: $3.10/M input, $15.50/M output (OpenRouter pass-through)
- Config field anthropic_api_key -> openrouter_api_key
- pyproject extras: anthropic>=0.40 -> openai>=1.50

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Viktor Barzin 2026-05-22 09:52:55 +00:00
parent 3c20c8c12c
commit 89f01ad9c0
5 changed files with 244 additions and 216 deletions

View file

@ -18,12 +18,12 @@ class MeetKevinWatcherConfig(BaseConfig):
# LLM analysis settings
meet_kevin_max_llm_retries: int = 3
meet_kevin_llm_model: str = "claude-sonnet-4-6"
meet_kevin_llm_model: str = "anthropic/claude-sonnet-4.5"
meet_kevin_prompt_version: str = "v1"
meet_kevin_daily_cost_cap_usd: float = 5.0
# API credentials
anthropic_api_key: str = ""
openrouter_api_key: str = ""
# Runtime settings
meet_kevin_workdir: str = "/tmp/meet_kevin_captions"

View file

@ -1,9 +1,7 @@
"""Claude LLM analyzer for Meet Kevin video transcripts.
"""OpenRouter LLM analyzer for Meet Kevin video transcripts.
Calls Claude Sonnet 4.6 with tool-use forcing to extract structured
MeetKevinAnalysis from a video transcript. Uses prompt caching on the
system block to reduce cost across videos processed within the same
5-minute window.
Calls Claude Sonnet (via OpenRouter) with function-calling forcing to extract
structured MeetKevinAnalysis from a video transcript.
Public API:
SYSTEM_PROMPT module-level analyst instructions
@ -12,13 +10,14 @@ Public API:
LlmAnalyzer async class; .analyze() does the API call
"""
import json
import logging
from dataclasses import dataclass
from datetime import datetime
from decimal import Decimal
from typing import Any
from anthropic import AsyncAnthropic
from openai import AsyncOpenAI
from shared.schemas.meet_kevin import MeetKevinAnalysis
@ -26,12 +25,16 @@ logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Pricing table (USD per 1 000 000 tokens: input, output)
# OpenRouter pass-through pricing (~3% markup over Anthropic list)
# ---------------------------------------------------------------------------
_PRICING: dict[str, tuple[Decimal, Decimal]] = {
"claude-sonnet-4-6": (Decimal("3"), Decimal("15")),
"claude-sonnet-4-6": (Decimal("3.10"), Decimal("15.50")),
"claude-opus-4-7": (Decimal("15"), Decimal("75")),
"claude-haiku-4-5-20251001": (Decimal("1"), Decimal("5")),
# OpenRouter model slugs
"anthropic/claude-sonnet-4.5": (Decimal("3.10"), Decimal("15.50")),
"anthropic/claude-sonnet-4.6": (Decimal("3.10"), Decimal("15.50")),
}
# ---------------------------------------------------------------------------
@ -138,97 +141,99 @@ Now read the transcript provided in the user message and call `submit_analysis`.
""".strip()
# ---------------------------------------------------------------------------
# Tool definition (JSON Schema mirroring MeetKevinAnalysis)
# Tool definition (OpenAI function-calling format)
# ---------------------------------------------------------------------------
_ANALYSIS_TOOL: dict[str, Any] = {
"name": "submit_analysis",
"description": (
"Submit a structured analysis of a Meet Kevin video transcript. "
"Call this exactly once with all fields filled in."
),
"input_schema": {
"type": "object",
"required": [
"market_outlook_direction",
"market_outlook_reasoning",
"macro_themes",
"key_risks",
"summary",
"tickers",
],
"properties": {
"market_outlook_direction": {
"type": "string",
"enum": ["bullish", "neutral", "bearish", "mixed"],
"description": "Overall market sentiment direction",
},
"market_outlook_reasoning": {
"type": "string",
"description": "2-4 sentence explanation of the market outlook direction",
},
"macro_themes": {
"type": "array",
"items": {"type": "string"},
"description": "2-6 high-level macro economic themes discussed",
},
"key_risks": {
"type": "array",
"items": {"type": "string"},
"description": "2-5 principal downside risks Kevin mentions",
},
"summary": {
"type": "string",
"description": "~200-word plain-English investment thesis summary",
},
"tickers": {
"type": "array",
"description": "Per-ticker mentions with action and conviction",
"items": {
"type": "object",
"required": [
"symbol",
"action",
"conviction",
"time_horizon",
"rationale_quote",
"video_timestamp_seconds",
],
"properties": {
"symbol": {
"type": "string",
"description": "Uppercase ticker symbol (1-6 chars)",
},
"action": {
"type": "string",
"enum": ["buy", "sell", "hold", "watch", "avoid"],
"description": "Recommendation action",
},
"conviction": {
"type": "number",
"minimum": 0.0,
"maximum": 1.0,
"description": "Confidence in recommendation (0.0-1.0)",
},
"time_horizon": {
"type": "string",
"enum": [
"intraday",
"days",
"weeks",
"months",
"long_term",
"unspecified",
],
"description": "Time horizon for the recommendation",
},
"rationale_quote": {
"type": "string",
"description": "Short verbatim or paraphrased quote from video",
},
"video_timestamp_seconds": {
"type": ["integer", "null"],
"description": "Timestamp in seconds for deep-link target",
_ANALYSIS_TOOL_OPENAI: dict[str, Any] = {
"type": "function",
"function": {
"name": "submit_analysis",
"description": (
"Submit the structured analysis of one Meet Kevin video. Call this exactly once."
),
"parameters": {
"type": "object",
"required": [
"market_outlook_direction",
"market_outlook_reasoning",
"macro_themes",
"key_risks",
"summary",
"tickers",
],
"properties": {
"market_outlook_direction": {
"type": "string",
"enum": ["bullish", "neutral", "bearish", "mixed"],
"description": "Overall market sentiment direction",
},
"market_outlook_reasoning": {
"type": "string",
"description": "2-4 sentence explanation of the market outlook direction",
},
"macro_themes": {
"type": "array",
"items": {"type": "string"},
"description": "2-6 high-level macro economic themes discussed",
},
"key_risks": {
"type": "array",
"items": {"type": "string"},
"description": "2-5 principal downside risks Kevin mentions",
},
"summary": {
"type": "string",
"description": "~200-word plain-English investment thesis summary",
},
"tickers": {
"type": "array",
"description": "Per-ticker mentions with action and conviction",
"items": {
"type": "object",
"required": [
"symbol",
"action",
"conviction",
"time_horizon",
"rationale_quote",
"video_timestamp_seconds",
],
"properties": {
"symbol": {
"type": "string",
"description": "Uppercase ticker symbol (1-6 chars)",
},
"action": {
"type": "string",
"enum": ["buy", "sell", "hold", "watch", "avoid"],
"description": "Recommendation action",
},
"conviction": {
"type": "number",
"minimum": 0.0,
"maximum": 1.0,
"description": "Confidence in recommendation (0.0-1.0)",
},
"time_horizon": {
"type": "string",
"enum": [
"intraday",
"days",
"weeks",
"months",
"long_term",
"unspecified",
],
"description": "Time horizon for the recommendation",
},
"rationale_quote": {
"type": "string",
"description": "Short verbatim or paraphrased quote from video",
},
"video_timestamp_seconds": {
"type": ["integer", "null"],
"description": "Timestamp in seconds for deep-link target",
},
},
},
},
@ -291,15 +296,15 @@ _MAX_SEGMENTS = 1000
class LlmAnalyzer:
"""Calls Claude to extract structured analysis from a video transcript.
"""Calls Claude (via OpenRouter) to extract structured analysis from a video transcript.
Args:
client: Configured AsyncAnthropic client.
model: Model identifier (e.g. "claude-sonnet-4-6").
client: Configured AsyncOpenAI client pointed at OpenRouter.
model: Model identifier (e.g. "anthropic/claude-sonnet-4.5").
prompt_version: Prompt version string stored in kevin_analyses.
"""
def __init__(self, client: AsyncAnthropic, model: str, prompt_version: str) -> None:
def __init__(self, client: AsyncOpenAI, model: str, prompt_version: str) -> None:
self._client = client
self._model = model
self._prompt_version = prompt_version
@ -313,7 +318,7 @@ class LlmAnalyzer:
transcript_text: str,
transcript_segments: list[dict],
) -> LlmCallResult:
"""Run Claude analysis on a transcript and return a structured result.
"""Run LLM analysis on a transcript and return a structured result.
Args:
title: Video title.
@ -326,8 +331,8 @@ class LlmAnalyzer:
LlmCallResult with parsed MeetKevinAnalysis and token accounting.
Raises:
ValueError: If the response contains no tool_use block.
pydantic.ValidationError: If tool_use input fails schema validation.
ValueError: If the response contains no tool_calls.
pydantic.ValidationError: If function arguments fail schema validation.
"""
user_msg = self._build_user_message(
title=title,
@ -337,42 +342,35 @@ class LlmAnalyzer:
transcript_segments=transcript_segments,
)
response = await self._client.messages.create(
response = await self._client.chat.completions.create(
model=self._model,
max_tokens=4096,
system=[
{
"type": "text",
"text": SYSTEM_PROMPT,
"cache_control": {"type": "ephemeral"},
}
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": user_msg},
],
tools=[_ANALYSIS_TOOL],
tool_choice={"type": "tool", "name": "submit_analysis"},
messages=[{"role": "user", "content": user_msg}],
tools=[_ANALYSIS_TOOL_OPENAI],
tool_choice={"type": "function", "function": {"name": "submit_analysis"}},
)
# Find the first tool_use block
tool_block = next(
(b for b in response.content if b.type == "tool_use"),
None,
)
if tool_block is None:
message = response.choices[0].message
if not message.tool_calls:
raise ValueError(
f"Claude response contained no tool_use block "
f"(stop_reason={response.stop_reason!r})"
"LLM response contained no tool_calls (expected submit_analysis function call)"
)
analysis = MeetKevinAnalysis.model_validate(tool_block.input)
tool_call = message.tool_calls[0]
tool_input = json.loads(tool_call.function.arguments)
analysis = MeetKevinAnalysis.model_validate(tool_input)
prompt_tokens: int = response.usage.input_tokens
completion_tokens: int = response.usage.output_tokens
prompt_tokens: int = response.usage.prompt_tokens
completion_tokens: int = response.usage.completion_tokens
cost_usd = compute_cost_usd(self._model, prompt_tokens, completion_tokens)
raw_response: dict = {
"stop_reason": response.stop_reason,
"tool_name": tool_block.name,
"tool_input": tool_block.input,
"finish_reason": response.choices[0].finish_reason,
"tool_name": tool_call.function.name,
"tool_input": tool_input,
"usage": {
"input_tokens": prompt_tokens,
"output_tokens": completion_tokens,

View file

@ -16,7 +16,7 @@ from datetime import timezone
from decimal import Decimal
import httpx
from anthropic import AsyncAnthropic
from openai import AsyncOpenAI
from sqlalchemy import select
from sqlalchemy.dialects.postgresql import insert as pg_insert
@ -179,10 +179,17 @@ async def run() -> None:
# Database
engine, session_factory = create_db(config)
# Anthropic client + LLM analyzer
anthropic = AsyncAnthropic(api_key=config.anthropic_api_key)
# OpenRouter client + LLM analyzer
client = AsyncOpenAI(
api_key=config.openrouter_api_key,
base_url="https://openrouter.ai/api/v1",
default_headers={
"HTTP-Referer": "https://trading.viktorbarzin.me",
"X-Title": "trading-bot meet-kevin",
},
)
analyzer = LlmAnalyzer(
client=anthropic,
client=client,
model=config.meet_kevin_llm_model,
prompt_version=config.meet_kevin_prompt_version,
)
@ -241,7 +248,7 @@ async def run() -> None:
except asyncio.TimeoutError:
pass # Normal timeout — loop again
finally:
await anthropic.close()
await client.close()
await engine.dispose()
logger.info("meet-kevin-watcher stopped gracefully")