> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cognisafe.uk/llms.txt
> Use this file to discover all available pages before exploring further.

# LM Studio

> Monitor local LM Studio inference through Cognisafe during development and testing.

## Overview

LM Studio is a desktop application for running LLMs locally on Mac, Windows, and Linux. It exposes a local OpenAI-compatible server, making it a natural fit for development and testing workflows where you want Cognisafe's safety scoring and observability without sending data to cloud providers.

## Start the LM Studio server

In LM Studio, go to **Local Server** → load a model → click **Start Server**.

By default it listens on `http://localhost:1234/v1`.

## Proxy configuration

```bash theme={null}
# Cognisafe proxy env vars
UPSTREAM_URL=http://host.docker.internal:1234
```

<Note>
  If running the Cognisafe proxy in Docker, use `host.docker.internal` (Mac/Windows) or your machine's LAN IP (Linux) to reach the LM Studio server on the host.
</Note>

If running the proxy directly on the same machine:

```bash theme={null}
UPSTREAM_URL=http://localhost:1234
```

## SDK setup

```python theme={null}
import cognisafe
from openai import OpenAI

cognisafe.configure(
    api_key="csk_your_key_here",
    project_id="local-dev",
    proxy_url="http://localhost:8080",
)
cognisafe.patch_openai()

client = OpenAI(
    api_key="lm-studio",  # LM Studio ignores the API key
    base_url="http://localhost:8080/v1",
)

response = client.chat.completions.create(
    model="lmstudio-community/Meta-Llama-3.1-8B-Instruct-GGUF",
    messages=[{"role": "user", "content": "Hello"}],
)
print(response.choices[0].message.content)
```

<Tip>
  Copy the exact model identifier from the LM Studio UI — it includes the GGUF file variant (e.g. `Q4_K_M`). The proxy passes the model name through unchanged.
</Tip>

## Docker Compose for local development

Add Cognisafe alongside LM Studio in a local `docker-compose.yml`:

```yaml theme={null}
services:
  cognisafe-proxy:
    image: cognisafe/proxy:latest
    ports:
      - "8080:8080"
    environment:
      UPSTREAM_URL: http://host.docker.internal:1234
      PROXY_API_KEY: dev-key
      API_BACKEND_URL: http://cognisafe-api:8000
    extra_hosts:
      - "host.docker.internal:host-gateway"  # Linux only

  cognisafe-api:
    image: cognisafe/api:latest
    ports:
      - "8000:8000"
    environment:
      POSTGRES_URL: postgresql+asyncpg://postgres:postgres@postgres:5432/cognisafe
      REDIS_URL: redis://redis:6379

  postgres:
    image: timescale/timescaledb:latest-pg16
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: cognisafe

  redis:
    image: redis:7-alpine
```

Point your app at `http://localhost:8080/v1` and LM Studio continues to run as normal on port 1234.

## Typical dev workflow

1. Load your model in LM Studio and start the local server
2. Start the Cognisafe proxy pointing at `localhost:1234`
3. Run your agent or app — all calls are logged and scored
4. Open the Cognisafe dashboard to inspect prompts, responses, and safety scores
5. When ready for production, change `UPSTREAM_URL` to your cloud LLM provider — no app code changes needed

<Tip>
  LM Studio is ideal for testing your safety scorer configuration. You can replay adversarial prompts against a local model to verify your custom scorers fire correctly before deploying to production.
</Tip>
