Skip to content

feat(async-client): add session pooling for connection reuse TAV-4105#155

Merged
pulvedu merged 3 commits into
masterfrom
feat/TAV-4105-session-pooling
Jan 30, 2026
Merged

feat(async-client): add session pooling for connection reuse TAV-4105#155
pulvedu merged 3 commits into
masterfrom
feat/TAV-4105-session-pooling

Conversation

@tinosattavily
Copy link
Copy Markdown
Contributor

@tinosattavily tinosattavily commented Jan 30, 2026

Summary

  • Add persistent httpx.AsyncClient instance for connection pooling in AsyncTavilyClient
  • Add close() method and async context manager support (__aenter__, __aexit__)
  • Enable TCP connection reuse across multiple API requests for improved performance
  • Add comprehensive test suite for session pooling edge cases (tests/test_session_pooling.py)
  • Bump version to 0.7.21

Changes

The async client now maintains a single httpx.AsyncClient instance instead of creating a new client per request. This enables HTTP connection reuse (avoiding TCP/TLS handshake overhead on subsequent requests).

Usage

# Option 1: Context manager (recommended)
async with AsyncTavilyClient(api_key="...") as client:
    result = await client.search("query")
    # Connection is reused for additional requests
    result2 = await client.search("another query")

# Option 2: Manual cleanup
client = AsyncTavilyClient(api_key="...")
try:
    result = await client.search("query")
finally:
    await client.close()

Test plan

  • All existing unit tests pass (30 tests)
  • New session pooling tests pass (tests/test_session_pooling.py)
  • Manual testing with real API calls

Linear: TAV-4105

🤖 Generated with Claude Code

tinosattavily and others added 3 commits January 28, 2026 17:16
- Replace per-request client creation with persistent httpx.AsyncClient
- Add close(), __aenter__, __aexit__ for lifecycle management
- Extract duplicated error handling into _handle_error_response()
- Add _post() helper for centralized request logic
- Consolidate _method/method pairs into single public methods
- Add test_session_pooling.py with 9 edge case tests
- Fix test_errors.py fixture crash when TAVILY_API_KEY unset

Performance: 2-10x faster for sequential requests (TCP connection reuse)
Code reduction: 786 → 476 lines (39% smaller)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
… pooling

Reverts helper method extraction (_handle_error_response, _post, etc.)
while preserving the core session pooling feature:
- Persistent self._client instance
- close(), __aenter__, __aexit__ lifecycle methods
- Direct self._client usage instead of _client_creator lambda

TAV-4105

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@pulvedu pulvedu merged commit c47f33f into master Jan 30, 2026
7 checks passed
@pulvedu pulvedu deleted the feat/TAV-4105-session-pooling branch January 30, 2026 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants