Skip to main content

Tex(...) constructor

Create one Tex client and reuse it. The same client handles remember, recall, token refresh, retries, and usage calls.
Tex(
    api_key: str | None = None,
    *,
    base_url: str | None = None,
    org_id: str | None = None,
    user_id: str | None = None,
    session_id: str | None = None,
    access_token: str | None = None,
    refresh_token: str | None = None,
    timeout: float = 60.0,
    max_retries: int = 2,
    http2: bool = True,
)
api_key
str
Your API key. Falls back to TEX_API_KEY env var.
base_url
str
Base URL of the Tex API. Falls back to TEX_BASE_URL. Required for production:
  • https://api.getmetacognition.com
org_id
str | None
Default org_id for all requests. Optional. The SDK auto-fills it from your JWT.
user_id
str | None
Default user_id. Set this for end-user partitioning in multi-tenant SaaS.
session_id
str | None
Default session_id. Most apps pass this per call.
access_token
str | None
Bring-your-own JWT. If set, the SDK skips the api_key exchange.
With BYO-JWT, the SDK does not auto-fill org_id / user_id from /auth/verify. Pass them to the constructor. remember and recall need them in the request scope.
refresh_token
str | None
Companion to access_token. Used for refresh on 401.
timeout
float
default:"60.0"
Per-request timeout in seconds.
max_retries
int
default:"2"
Retries transient errors: 408, 429, 5xx, and network failures.
http2
bool
default:"true"
HTTP/2 multiplexing. Disable if your egress proxy strips it.

Environment variables

VariablePurpose
TEX_API_KEYRead by the constructor when api_key= is omitted.
TEX_BASE_URLRead by the constructor when base_url= is omitted.
A .env template:
TEX_API_KEY=tex_live_xxxxxxxxxxxxxxxxxxxxxxxx
TEX_BASE_URL=https://api.getmetacognition.com

Lifecycle

The client keeps a pooled httpx.Client under the hood. Construct once, reuse everywhere.
# settings.py
from functools import cache
from tex import Tex
import os

@cache
def tex() -> Tex:
    return Tex(
        api_key=os.environ["TEX_API_KEY"],
        base_url=os.environ["TEX_BASE_URL"],
    )

Concurrency

The client is thread-safe for read traffic (recall, usage.today). For high write volume, push remember calls to a worker pool:
from concurrent.futures import ThreadPoolExecutor
pool = ThreadPoolExecutor(max_workers=16)

def remember_async(turns, sid):
    pool.submit(tex.conversations.remember, turns=turns, session_id=sid)
A native async client is on the roadmap.

Closing

tex.close()   # closes the underlying httpx.Client
Or use the context manager pattern above. __exit__ calls close().

Next: Remember

Push conversation turns into memory.