Metadata-Version: 2.4
Name: gridline
Version: 0.2.0
Summary: Name a role; configuration decides what serves it.
Requires-Python: >=3.11
Requires-Dist: httpx>=0.27
Description-Content-Type: text/markdown

# Gridline

Application code names a role. Configuration decides which model serves it, what it can
reach for, and what happens when a provider goes down.

There are two ways in, and the first one asks less of you.

---

## 1. Keep the client you already have

Gridline speaks the providers' own APIs and, on request, answers in their own bytes. So
point your existing client at it, name a Gridline agent in the model string, and stop.

```bash
curl https://gridline.internal/v1/messages \
  -H "authorization: Bearer $GRIDLINE_KEY" \
  -H "content-type: application/json" \
  -d '{
        "model": "gridline/invoice-reconciler",
        "format": "raw",
        "max_tokens": 1024,
        "messages": [{"role": "user", "content": "What did we agree about the VAT deadline?"}]
      }'
```

Three things are happening there:

- **`model: "gridline/<agent>"`** names the agent. The agent's configuration decides the
  real model and its fallbacks, so nothing in your code names a vendor.
- **`format: "raw"`** returns the provider's own response shape, and on an ordinary turn its exact bytes (a turn the proxy ran a tool loop for is assembled from several calls, so it is provider-shaped rather than byte-identical -- see docs/30), so your SDK's
  parser keeps working. Leave it out and you get the canonical shape below instead —
  which is a third shape, and a strict client will reject it.
- **`authorization: Bearer`** is a Gridline route key, not a provider key. Provider
  credentials stay in your own secret manager and are resolved in the data plane.

The same request from the official Anthropic SDK, LangChain, or Vercel's
`@ai-sdk/anthropic` is a base URL and a model string. Worked examples for each, plus what
this route cannot do — approvals, mainly —
are in [`docs/30-passthrough.md`](../docs/30-passthrough.md). Read the last section of
that page before shipping it.

---

## 2. Or use this library

For approvals, one response shape across every provider, and sessions handled for you.

```bash
uv pip install -e ./sdk    # nothing is published yet; this is the repository copy
```

```python
import gridline

grid = gridline.connect("https://gridline.internal", key="gl_...")
chat = grid.session(
    agent="invoice-reconciler",
    assign={"memory": {"store": "acme-ledgers", "subject": "user-123"}},
)

answer = await chat.send("What did we agree about the VAT deadline?")
print(answer.text)
```

**It is asynchronous, and that is the default rather than the advanced option.** A turn
is almost entirely spent waiting on a model, and a turn that runs tools waits several
times, so blocking a thread for it is the wrong shape in anything serving requests.

If you want the blocking one, that is one argument and nothing else changes:

```python
grid = gridline.connect("https://gridline.internal", key="gl_...", sync=True)
answer = chat.send("What did we agree about the VAT deadline?")   # no await
```

Every other name is identical between them — `send`, `stream`, `resume`, the reply, and
`.text`/`.reply`/`.tool_calls`/`.session` on a stream — so moving a handler from one to
the other is adding or deleting `async` and `await`. That choice is usually made after
the code is written, and it should not be a rewrite when it is.

A session is a conversation, not a connection. It holds nothing the proxy does not also
hold, so `grid.session(agent=..., resume=answer.session)` in another process carries on
where this one stopped.

**One reply shape, whoever served it.** `answer.text`, `answer.tool_calls`,
`answer.usage`, `answer.stop_reason` — and `answer.raw` is still exactly what the
provider sent, for the days you need a vendor detail this shape does not model.

**Degradation is readable rather than silent.** `answer.degraded` is true when the turn
got less than it was configured for; `route_degraded`, `session_degraded` and
`tools_degraded` say which, because a fallback model and a lost memory are not the same
problem. `answer.context_compacted` sits beside them rather than inside: a shortened
conversation is the harness working as configured, not a shortfall, but the model still
answered from less than you sent and nothing else in the reply says so.

**Retries are narrow on purpose.** A turn is not idempotent, so the client only repeats
what it knows did not run — a connection that was never established, a 429, a 503. A
read timeout is never retried, because the request arrived and something is working on
it. Pass `retry=gridline.Retry(attempts=5)` to widen it, or `gridline.NO_RETRY` to turn
it off.

**Approvals.** A tool configured to `ask` pauses the turn instead of running:

```python
answer = chat.send("post the January invoices")
if answer.awaiting_approval:
    for request in answer.approvals:
        print(request.name, request.arguments)      # a decision, not a button
    answer = chat.resume({r.id: True for r in answer.approvals})
```

The id is the *call's*, not the tool's: approving this `post_invoice` with these
arguments is something a person can be accountable for.

**Streaming.**

```python
with chat.stream("summarise the January ledger") as answer:
    for piece in answer:
        print(piece, end="", flush=True)
    print(answer.reply.usage.output_tokens)
```

Same arguments as `send`. Switching between them is adding or deleting a `for` loop.

---

## Which one

Take route 1 if you have an application already and want routing, failover and cost
attribution underneath it today. Take route 2 if a tool needs approving before it runs,
if you want one parsing path across providers, or if a degraded turn has to reach a
person. Nothing you configure differs between them, and running both against the same
agent is normal.
