Implementation:Togethercomputer Together python Together Client Init
Appearance
| Attribute | Value |
|---|---|
| Implementation Name | Together_Client_Init |
| Overview | Synchronous and asynchronous API client constructors that configure authentication, transport, and resource namespaces. |
| Source File | src/together/client.py |
| Lines | L16-98 (Together), L101-181 (AsyncTogether) |
| Domain | NLP, API_Client, Inference |
| Repository | togethercomputer/together-python |
| Last Updated | 2026-02-15 16:00 GMT |
Code Reference
class Together:
completions: resources.Completions
chat: resources.Chat
embeddings: resources.Embeddings
files: resources.Files
images: resources.Images
models: resources.Models
fine_tuning: resources.FineTuning
rerank: resources.Rerank
audio: resources.Audio
batches: resources.Batches
code_interpreter: CodeInterpreter
evaluation: resources.Evaluation
videos: resources.Videos
client: TogetherClient
def __init__(
self,
*,
api_key: str | None = None,
base_url: str | None = None,
timeout: float | None = None,
max_retries: int | None = None,
supplied_headers: Dict[str, str] | None = None,
) -> None:
...
The AsyncTogether class mirrors the same constructor signature, using async resource variants (resources.AsyncCompletions, resources.AsyncChat, etc.).
Import
from together import Together, AsyncTogether
API Signature
Together(
*,
api_key: str | None = None,
base_url: str | None = None,
timeout: float | None = None,
max_retries: int | None = None,
supplied_headers: Dict[str, str] | None = None,
) -> None
AsyncTogether(
*,
api_key: str | None = None,
base_url: str | None = None,
timeout: float | None = None,
max_retries: int | None = None,
supplied_headers: Dict[str, str] | None = None,
) -> None
I/O Contract
Input Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
api_key |
None | None |
Together API key. Falls back to TOGETHER_API_KEY env var, then Google Colab secret. Raises AuthenticationError if unresolvable.
|
base_url |
None | None |
API base URL. Falls back to TOGETHER_BASE_URL env var, then https://api.together.xyz/v1.
|
timeout |
None | None |
Request timeout in seconds. Defaults to TIMEOUT_SECS (600 seconds).
|
max_retries |
None | None |
Maximum number of retries for failed requests. Defaults to MAX_RETRIES (5).
|
supplied_headers |
None | None |
Additional HTTP headers to include with every request. |
Output
A configured client instance with the following resource attributes:
| Attribute | Type (Sync) | Type (Async) | Description |
|---|---|---|---|
.chat |
resources.Chat |
resources.AsyncChat |
Chat completions API |
.completions |
resources.Completions |
resources.AsyncCompletions |
Text completions API |
.embeddings |
resources.Embeddings |
resources.AsyncEmbeddings |
Embeddings API |
.files |
resources.Files |
resources.AsyncFiles |
File management API |
.images |
resources.Images |
resources.AsyncImages |
Image generation API |
.models |
resources.Models |
resources.AsyncModels |
Model listing API |
.fine_tuning |
resources.FineTuning |
resources.AsyncFineTuning |
Fine-tuning API |
.rerank |
resources.Rerank |
resources.AsyncRerank |
Reranking API |
.audio |
resources.Audio |
resources.AsyncAudio |
Audio API |
.batches |
resources.Batches |
resources.AsyncBatches |
Batch jobs API |
.code_interpreter |
CodeInterpreter |
CodeInterpreter |
Code interpreter API |
.evaluation |
resources.Evaluation |
resources.AsyncEvaluation |
Evaluation API |
.videos |
resources.Videos |
resources.AsyncVideos |
Video generation API |
Usage Examples
Basic Initialization
from together import Together
# API key from environment variable TOGETHER_API_KEY
client = Together()
# Explicit API key
client = Together(api_key="your-api-key-here")
Custom Configuration
from together import Together
client = Together(
api_key="your-api-key-here",
base_url="https://custom-endpoint.example.com/v1",
timeout=120.0,
max_retries=3,
supplied_headers={"X-Custom-Header": "value"},
)
Async Client
import asyncio
from together import AsyncTogether
async def main():
client = AsyncTogether()
response = await client.chat.completions.create(
model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)
asyncio.run(main())
API Key Resolution Chain
import os
from together import Together
from together.error import AuthenticationError
# 1. Explicit api_key argument (highest priority)
client = Together(api_key="explicit-key")
# 2. TOGETHER_API_KEY environment variable
os.environ["TOGETHER_API_KEY"] = "env-key"
client = Together()
# 3. Google Colab secret (automatic in Colab notebooks)
# 4. No key found -> raises AuthenticationError
try:
del os.environ["TOGETHER_API_KEY"]
client = Together()
except AuthenticationError as e:
print(f"Authentication failed: {e}")
Key Implementation Details
- The constructor is keyword-only (note the bare
*in the signature) -- positional arguments are not accepted. - The base URL is normalized with
enforce_trailing_slash()before being stored. - All resource objects are initialized eagerly in the constructor, receiving the shared
TogetherClientconfiguration object. - The
Togetherclass is also aliased asClient, andAsyncTogetherasAsyncClient(see L184-186 ofclient.py). - Default constants from
src/together/constants.py:TIMEOUT_SECS = 600MAX_RETRIES = 5BASE_URL = "https://api.together.xyz/v1"
Related
- Principle:Togethercomputer_Together_python_Client_Initialization
- Implementation:Togethercomputer_Together_python_ChatCompletions_Create
- Implementation:Togethercomputer_Together_python_Together_Exception_Hierarchy
- Environment:Togethercomputer_Together_python_Python_SDK_Runtime
- Environment:Togethercomputer_Together_python_API_Credentials
- Heuristic:Togethercomputer_Together_python_Retry_Backoff_Strategy
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment