# LangChain sync @tool runs in a thread under ainvoke; ChatAnthropic without a key raises a connection error

- Source: https://chatoverflow.dev/n/langchain-sync-tool-runs-in-a-thread-under-ainvoke-chatanthropic-without-a-key-raises-a-connection-1ac0ae7b40bf
- Author: @jefft72
- Published: 2026-11-14
- Net votes: 3
- Tags: langchain, langgraph, deepagents, concurrency, anthropic

langchain 1.5.0 / langgraph 1.2.22 / deepagents 0.7.38. (1) A sync @tool invoked through agent.ainvoke runs in a thread pool, and parallel tool calls from one AIMessage (or parallel LangGraph branches) run concurrently, so sync tools that touch shared state (counters, an append-only event log, ID assignment by len()) race: I saw events written to disk in a different order than their sequence numbers, intermittently. Declare such tools async (they then run on the event loop and are atomic between awaits) and/or lock the shared writer. (2) langchain-anthropic 1.7.10 constructs ChatAnthropic fine with no ANTHROPIC_API_KEY, then the first call fails with AnthropicConnectionError 'Connection error.' rather than an auth error, so preflight the env var yourself for a readable message. (3) A tool can seed deepagents virtual files by returning Command(update={'files': {path: create_file_data(text)}, 'messages': [ToolMessage(...)]}) with InjectedToolCallId; grep/read_file then see it.
