> ## Documentation Index
> Fetch the complete documentation index at: https://metacognition-fdc534de-master.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Configure the client

> Set API keys, base URL, timeouts, retries, and client lifetime.

## `Tex(...)` constructor

Create one **`Tex`** client and reuse it. The same client handles **`remember`**, **`recall`**, token refresh, retries, and usage calls.

```python theme={null}
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,
)
```

<ParamField path="api_key" type="str">
  Your API key. Falls back to `TEX_API_KEY` env var.
</ParamField>

<ParamField path="base_url" type="str">
  Base URL of the Tex API. Falls back to `TEX_BASE_URL`. Required for production:

  * `https://api.getmetacognition.com`
</ParamField>

<ParamField path="org_id" type="str | None">
  Default `org_id` for all requests. Optional. The SDK auto-fills it from your JWT.
</ParamField>

<ParamField path="user_id" type="str | None">
  Default `user_id`. Set this for end-user partitioning in multi-tenant SaaS.
</ParamField>

<ParamField path="session_id" type="str | None">
  Default `session_id`. Most apps pass this per call.
</ParamField>

<ParamField path="access_token" type="str | None">
  Bring-your-own JWT. If set, the SDK skips the `api_key` exchange.

  <Warning>
    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`.
  </Warning>
</ParamField>

<ParamField path="refresh_token" type="str | None">
  Companion to `access_token`. Used for refresh on 401.
</ParamField>

<ParamField path="timeout" type="float" default="60.0">
  Per-request timeout in seconds.
</ParamField>

<ParamField path="max_retries" type="int" default="2">
  Retries transient errors: 408, 429, 5xx, and network failures.
</ParamField>

<ParamField path="http2" type="bool" default="true">
  HTTP/2 multiplexing. Disable if your egress proxy strips it.
</ParamField>

## Environment variables

| Variable       | Purpose                                              |
| -------------- | ---------------------------------------------------- |
| `TEX_API_KEY`  | Read by the constructor when `api_key=` is omitted.  |
| `TEX_BASE_URL` | Read by the constructor when `base_url=` is omitted. |

A `.env` template:

```bash theme={null}
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.**

<Tabs>
  <Tab title="✅ Module-level">
    ```python theme={null}
    # 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"],
        )
    ```
  </Tab>

  <Tab title="✅ Context manager">
    ```python theme={null}
    with Tex(api_key=...) as tex:
        tex.recall(q="...", session_id="...")
    # connection is closed on exit
    ```
  </Tab>

  <Tab title="❌ Per-request">
    ```python theme={null}
    @app.post("/chat")
    def chat(req):
        tex = Tex(api_key=...)   # opens a new TLS session every request
        return tex.recall(...)
    ```

    This opens a new TCP and TLS connection on every call.
  </Tab>
</Tabs>

## Concurrency

The client is **thread-safe for read traffic** (`recall`, `usage.today`).

For high write volume, push **`remember`** calls to a worker pool:

```python theme={null}
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

```python theme={null}
tex.close()   # closes the underlying httpx.Client
```

Or use the context manager pattern above. `__exit__` calls `close()`.

<Card title="Next: Remember" icon="comments" href="/sdk/conversations-remember">
  Push conversation turns into memory.
</Card>
