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.
Build with agent frameworks
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 | You already build graphs, chains, or RAG pipelines and want sference as the model provider |
| OpenAI Agents SDK | OpenAI Agents SDK | Handoffs, guardrails, and tracing; OpenAI-native agent primitives |
| Anthropic Agent SDK | 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.
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 |
Flex and batch are 0.7× and 0.5× list price respectively, and they stack. See Pricing.
# 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:
- Pin the worker with
prompt_cache_key. An agent resends the same system prompt and tool definitions on every step. Setprompt_cache_keyto the run id so those tokens bill at the cached rate instead of full price. See Prompt caching. - Bound the wait with
max_queue_wait_s. Fail over rather than hang when the fleet is busy. See Max queue wait. - Cap
max_output_tokenson reasoning models. The cap covers hidden reasoning, so an uncapped thinking step can burn the budget before it acts. - 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
ChatOpenAI, LangGraph, and tool-calling agents on sference.
OpenAI Agents SDK
Responses API, handoffs, and background steps.
Anthropic Agent SDK
Claude Agent SDK and the anthropic SDK on open models.
Structured outputs
Constrain a step's output to a JSON schema.
Requests at scale
Fan out concurrent background responses, or submit one batch.
SferenceCode
Run a coding agent on sference with one command.