Mastra, the AI SDK, LangGraph.js and the OpenAI Agents SDK compared for TypeScript teams shipping agents, plus how Encore adds the database, queues and secrets the agent's backend needs.
TypeScript has the widest choice of agent frameworks after Python, and most of them share the same building blocks, since the AI SDK handles model calls, streaming and tools for many higher-level frameworks.
The framework decides how the agent reasons and calls tools, while the backend around it decides whether a run survives a deploy, where conversations are stored and who can read the API keys. Usage keeps growing, with 84 percent of respondents to the 2025 Stack Overflow Developer Survey using or planning to use AI tools in their development process.
What to weigh when choosing:
The AI SDK offers generateText, streamText and tool definitions with Zod schemas across many providers, plus UI hooks for streaming chat.
It is a toolkit rather than a framework, so memory, workflows and persistence are yours to build.
Mastra adds agents, workflows, memory, RAG and evals on top of the AI SDK, with a local playground for running agents.
Mastra makes many decisions for you, and its memory and workflow state need a storage backend in each environment.
LangGraph.js is the TypeScript version of LangGraph, modeling agents as graphs with shared state and checkpointers.
The graph model is more to learn than a tool loop, and the checkpoint store is another database to run.
The OpenAI Agents SDK for TypeScript provides agents, tools, handoffs, guardrails and tracing.
It is built around OpenAI's APIs first, and its default tracing is sent to OpenAI.
Encore combines the agent's TypeScript backend with its infrastructure through an open source infrastructure SDK for TypeScript, where the agent's service declares a Postgres database, a Pub/Sub topic and secrets in code, and the agent framework of your choice runs inside the handlers. encore run starts Postgres locally with the topic, and a deploy provisions both in your own AWS or GCP account. Whichever option above you pick, Encore can add the database, topics and secrets to the same app.
import { api } from "encore.dev/api";
import { secret } from "encore.dev/config";
import { Subscription, Topic } from "encore.dev/pubsub";
import { SQLDatabase } from "encore.dev/storage/sqldb";
import { createOpenAI } from "@ai-sdk/openai";
import { generateText } from "ai";
const db = new SQLDatabase("agent", { migrations: "./migrations" });
const openaiKey = secret("OpenAIKey");
interface RunRequested {
runId: number;
prompt: string;
}
export const runs = new Topic<RunRequested>("agent-runs", {
deliveryGuarantee: "at-least-once",
});
const _ = new Subscription(runs, "execute-run", {
handler: async ({ runId, prompt }) => {
const openai = createOpenAI({ apiKey: openaiKey() });
const { text } = await generateText({ model: openai("gpt-4o"), prompt });
await db.exec`UPDATE runs SET output = ${text}, done = true WHERE id = ${runId}`;
},
});
export const createRun = api(
{ expose: true, method: "POST", path: "/runs" },
async ({ prompt }: { prompt: string }): Promise<{ runId: number }> => {
const row = await db.queryRow<{ id: number }>`
INSERT INTO runs (prompt) VALUES (${prompt}) RETURNING id`;
await runs.publish({ runId: row!.id, prompt });
return { runId: row!.id };
},
);
encore run serves it locally right away.Encore leaves the model loop to your agent framework. Cloud resources are created at deploy time, and the cloud access sits with Encore's IAM role, not the agent. For a complete walkthrough, see how to build an AI agent backend with TypeScript.
Anthropic's building effective agents guide found that the most successful teams used simple, composable patterns, and a METR randomized trial found AI tools made experienced developers 19 percent slower on real tasks, so plan for review and keep a record of every run.
The AI SDK is the most common base for model calls, streaming and tool use in TypeScript. Mastra builds agents, workflows and memory on top of it, LangGraph.js suits agents modeled as stateful graphs, and the OpenAI Agents SDK suits handoffs between agents on OpenAI models. Encore is not an agent framework but an infrastructure SDK for the backend that runs the agent, covering its database, Pub/Sub topics and secrets.
Short agent calls can, especially when the response is streamed. Runs that call several tools or wait on slow APIs are safer in a background worker that retries on failure, with the result stored for the client to fetch.
Declare a Topic and a Subscription, publish a message from the API endpoint that receives the request, and call your agent framework in the subscription handler. Store the result in the SQLDatabase the service declares, and read the model API key from an Encore secret.
Deploy to your cloud on AWS and Google Cloud
without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.