Coverage for src/copytrading_engine/parsing/providers/anthropic.py: 69%

33 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-16 16:55 +0000

1"""Anthropic's adapter. The reading's schema goes in the instructions and the answer is checked 

2like any other: Anthropic compiles a strict output schema into a grammar, and the reading's is too 

3large for it on every Claude model, and newer models refuse a forced tool call.""" 

4 

5import logging 

6 

7import httpx2 as httpx 

8from anthropic import APIConnectionError, AsyncAnthropic 

9from pydantic_ai import Agent, PromptedOutput 

10from pydantic_ai.models.anthropic import AnthropicModel 

11from pydantic_ai.providers.anthropic import AnthropicProvider 

12 

13from copytrading_engine.parsing.learning import LEARN_INSTRUCTIONS, PlaybookProposal 

14from copytrading_engine.shared.cleanup import close_logged 

15from copytrading_engine.shared.model_providers import ProviderConfig 

16from copytrading_engine.shared.payload_capture import PayloadCapture 

17 

18from .pydantic_ai import PydanticAIDecoder, reading_agent 

19from .registry import ManagedDecoder 

20from .transport import DiagnosticHTTPTransport 

21 

22log = logging.getLogger(__name__) 

23 

24 

25def client(config: ProviderConfig, http_client: httpx.AsyncClient | None = None) -> AsyncAnthropic: 

26 return AsyncAnthropic( 

27 api_key=config.api_key.get_secret_value(), 

28 max_retries=0, 

29 timeout=config.timeout, 

30 http_client=http_client, 

31 ) 

32 

33 

34def chat_model(config: ProviderConfig, client: AsyncAnthropic) -> AnthropicModel: 

35 """The model the decoder reads with, which the assistant also chats with.""" 

36 return AnthropicModel(config.model, provider=AnthropicProvider(anthropic_client=client)) 

37 

38 

39def build_decoder(config: ProviderConfig, client: AsyncAnthropic) -> PydanticAIDecoder: 

40 model = chat_model(config, client) 

41 agent = reading_agent( 

42 model, 

43 {"temperature": 0, "max_tokens": 3000}, 

44 ) 

45 learner = Agent( 

46 model, 

47 output_type=PromptedOutput(PlaybookProposal), 

48 name="playbook_learner", 

49 instructions=LEARN_INSTRUCTIONS, 

50 retries=1, 

51 model_settings={"temperature": 0, "max_tokens": 8000}, 

52 ) 

53 return PydanticAIDecoder( 

54 agent, learner, client, config.timeout, connection_errors=(APIConnectionError,) 

55 ) 

56 

57 

58async def create_decoder( 

59 config: ProviderConfig, 

60 *, 

61 diagnostics: PayloadCapture | None = None, 

62) -> ManagedDecoder: 

63 http_client = None 

64 if diagnostics is not None: 

65 http_client = httpx.AsyncClient( 

66 transport=DiagnosticHTTPTransport( 

67 httpx.AsyncHTTPTransport(retries=0), diagnostics, provider="anthropic" 

68 ), 

69 timeout=config.timeout, 

70 follow_redirects=False, 

71 ) 

72 opened = client(config, http_client) 

73 try: 

74 return build_decoder(config, opened) 

75 except BaseException: 

76 await close_logged(opened.close, resource="provider_client", log=log) 

77 raise