# Build with agent frameworks

> Build agent loops on sference with LangChain, the OpenAI Agents SDK, and the Anthropic Agent SDK — and pick the window each step runs on.

Source: https://sference.com/docs/agents

An agent loop is many model calls in sequence: plan, call a tool, read the result, decide again. sference is built for that shape. The turn a user is waiting on runs **realtime**; the steps behind it can run on **flex** or the **24h batch window** — same API, same fleet, same audit trail, one bill.

This section shows how to point three popular agent frameworks at sference, how to choose a window per step, and the two patterns every agent needs in production: **structured output** and **sending requests at scale**.

| Framework | Page | Why you would pick it |
| --- | --- | --- |
| **LangChain / LangGraph** | [LangChain](https://sference.com/docs/agents/langchain) | You already build graphs, chains, or RAG pipelines and want sference as the model provider |
| **OpenAI Agents SDK** | [OpenAI Agents SDK](https://sference.com/docs/agents/openai-agents-sdk) | Handoffs, guardrails, and tracing; OpenAI-native agent primitives |
| **Anthropic Agent SDK** | [Anthropic Agent SDK](https://sference.com/docs/agents/anthropic-agent-sdk) | You use the Claude Agent SDK or the `anthropic` SDK's tool runner and want open models behind it |

## One base URL, three SDKs

Every framework below reaches sference the same way: an OpenAI-compatible or Anthropic-compatible base URL plus a `sk_...` key. Nothing else about the framework changes.

```python
import os

BASE_URL = "https://api.sference.com/v1"
API_KEY = os.environ["SFERENCE_API_KEY"]
```

| Framework | Surface it uses | Base URL |
| --- | --- | --- |
| LangChain (`ChatOpenAI`) | `/v1/chat/completions` | `https://api.sference.com/v1` |
| OpenAI Agents SDK | `/v1/responses` | `https://api.sference.com/v1` |
| Anthropic Agent SDK | `/v1/messages` | `https://api.sference.com` |

> **Model ids are catalog ids**
>
> There are no `gpt-*` or `claude-*` aliases. Pass a sference catalog id such as `zai-org/GLM-5.3` or `moonshotai/Kimi-K3`. `GET https://api.sference.com/v1/models` lists what your key can call.

## Choosing a window per step

The single biggest cost decision in an agent is **which steps need to be synchronous**. Most do not.

| Step | Window | How |
| --- | --- | --- |
| User-facing turn, tool selection the user is waiting on | realtime | default sync call |
| Background research, retrieval, summarization | flex | `service_tier: "flex"` |
| Long-horizon steps, overnight jobs, evals | batch | `background: true` + `metadata.completion_window: "24h"` |
| Bulk fan-out of many independent steps | batch | [Batch API](https://sference.com/docs/guides/batches) |

Flex and batch are **0.7×** and **0.5×** list price respectively, and they stack. See [Pricing](https://sference.com/docs/pricing).

```python
# The turn the user waits on — realtime
answer = client.chat.completions.create(model=MODEL, messages=messages)

# A background step nobody is waiting on — flex, 30% cheaper
summary = client.chat.completions.create(
    model=MODEL,
    messages=[{"role": "user", "content": doc}],
    service_tier="flex",
)

# An overnight job — batch, 50% cheaper
job = client.responses.create(
    model=MODEL,
    input=task,
    background=True,
    metadata={"completion_window": "24h"},
)
```

## Keeping an agent loop cheap and fast

Four practices apply to every framework on this page:

1. **Pin the worker with `prompt_cache_key`.** An agent resends the same system prompt and tool definitions on every step. Set `prompt_cache_key` to the run id so those tokens bill at the cached rate instead of full price. See [Prompt caching](https://sference.com/docs/guides/prompt-caching).
2. **Bound the wait with `max_queue_wait_s`.** Fail over rather than hang when the fleet is busy. See [Max queue wait](https://sference.com/docs/guides/max-queue-wait).
3. **Cap `max_output_tokens` on reasoning models.** The cap covers hidden reasoning, so an uncapped thinking step can burn the budget before it acts.
4. **Put the volatile part of the prompt last.** Timestamps, retrieved documents, and the user's question go after the stable system prompt.

## Where to go next

- [LangChain](https://sference.com/docs/agents/langchain): ChatOpenAI, LangGraph, and tool-calling agents on sference.
- [OpenAI Agents SDK](https://sference.com/docs/agents/openai-agents-sdk): Responses API, handoffs, and background steps.
- [Anthropic Agent SDK](https://sference.com/docs/agents/anthropic-agent-sdk): Claude Agent SDK and the anthropic SDK on open models.
- [Structured outputs](https://sference.com/docs/guides/structured-outputs): Constrain a step's output to a JSON schema.
- [Requests at scale](https://sference.com/docs/guides/requests-at-scale): Fan out concurrent background responses, or submit one batch.
- [SferenceCode](https://sference.com/docs/sference-code): Run a coding agent on sference with one command.
