my prefect server setup prefect-metrics.waow.tech
python orchestration
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: defer agent construction until API key is loaded

AnthropicProvider validates the API key at __init__ time, so the agent
can't be created at module level before the Prefect Secret is available.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

zzstoatzz e31ae075 627c97b7

+20 -17
+20 -17
flows/curate.py
··· 5 5 import duckdb 6 6 from pydantic_ai import Agent 7 7 from pydantic_ai.durable_exec.prefect import PrefectAgent, TaskConfig 8 + from pydantic_ai.providers.anthropic import AnthropicProvider 8 9 from prefect import flow, task, get_run_logger 9 10 from prefect.blocks.system import Secret 10 11 ··· 22 23 the headline should be a single sentence summary. 23 24 """ 24 25 25 - agent = Agent( 26 - "anthropic:claude-haiku-4-5", 27 - output_type=Briefing, 28 - system_prompt=SYSTEM_PROMPT, 29 - name="hub-curator", 30 - ) 31 - 32 - prefect_agent = PrefectAgent( 33 - agent, 34 - model_task_config=TaskConfig( 35 - retries=2, 36 - retry_delay_seconds=[2.0, 5.0], 37 - ), 38 - ) 26 + def make_agent(api_key: str) -> PrefectAgent[Briefing]: 27 + """Build agent after API key is available (provider validates key at init).""" 28 + agent = Agent( 29 + "anthropic:claude-haiku-4-5", 30 + output_type=Briefing, 31 + system_prompt=SYSTEM_PROMPT, 32 + name="hub-curator", 33 + provider=AnthropicProvider(api_key=api_key), 34 + ) 35 + return PrefectAgent( 36 + agent, 37 + model_task_config=TaskConfig( 38 + retries=2, 39 + retry_delay_seconds=[2.0, 5.0], 40 + ), 41 + ) 39 42 40 43 41 44 @task ··· 79 82 str(Path(db_path).parent / "briefing.json"), 80 83 ) 81 84 82 - # set API key from Prefect Secret 83 - api_key = await Secret.load("anthropic-api-key") 84 - os.environ["ANTHROPIC_API_KEY"] = api_key.get() 85 + # load API key from Prefect Secret, build agent 86 + api_key = (await Secret.load("anthropic-api-key")).get() 87 + prefect_agent = make_agent(api_key) 85 88 86 89 items_text = load_items(db_path) 87 90 logger.info(f"loaded {items_text.count(chr(10)) + 1} items for curation")