Principle:Togethercomputer Together python Client Initialization
| Attribute | Value |
|---|---|
| Principle Name | Client_Initialization |
| Overview | Mechanism for creating authenticated API client instances that provide access to all Together AI services. |
| Domain | NLP, API_Client, Inference |
| Repository | togethercomputer/together-python |
| Last Updated | 2026-02-15 16:00 GMT |
Description
Client initialization is the process of creating a configured API client that manages authentication, base URL routing, timeout settings, and retry policies. The Together SDK provides both synchronous (Together) and asynchronous (AsyncTogether) client variants that share the same constructor signature and configuration semantics.
The client object serves as the single entry point for all Together AI API interactions. Once initialized, it exposes namespaced resource attributes (such as .chat, .completions, .embeddings, .images, and others) that encapsulate the HTTP request logic for each API capability.
Authentication is resolved through a fallback chain:
- An explicit
api_keykeyword argument takes highest priority. - If not provided, the
TOGETHER_API_KEYenvironment variable is checked. - In Google Colab environments, the Colab secrets manager is queried as a final fallback.
- If no API key can be resolved, an
AuthenticationErroris raised immediately.
The base URL, timeout, and retry settings follow a similar pattern of explicit argument taking priority over environment variable fallback, with sensible defaults applied when neither is specified.
Usage
Use client initialization when starting any interaction with Together AI services. It is the required first step before making any API call, including chat completions, embeddings, image generation, file management, fine-tuning, and model listing.
When to use:
- At application startup to create a shared client instance
- When switching between different API keys or base URLs
- When configuring custom timeout or retry behavior for specific workloads
- When using async frameworks that require the
AsyncTogethervariant
When not to use:
- Do not create a new client for every API call; reuse a single client instance throughout the application lifetime
- Do not use
AsyncTogetherin synchronous code paths (and vice versa)
Theoretical Basis
The client follows the resource composition pattern -- a single entry point object that eagerly initializes namespaced resource objects for each API capability. Each resource (e.g., chat, completions, embeddings) is a dedicated class that receives the shared TogetherClient configuration object containing authentication credentials, base URL, timeout, and retry settings.
This pattern provides several advantages:
- Single point of configuration: All API calls inherit the same authentication and transport settings.
- Discoverability: IDE autocompletion on the client object reveals all available API capabilities.
- Testability: A single mock or stub of the client can intercept all API calls.
The fallback chain for API key resolution (explicit argument, environment variable, platform secret) follows the principle of least surprise, matching the conventions established by the OpenAI Python SDK and similar API clients.
Knowledge Sources
| Source | Type | URI |
|---|---|---|
| Together AI API Documentation | Doc | Together AI Quickstart |
| Together AI API Keys | Doc | Together AI Authentication |