Oct 23, 20266 min read

Best TypeScript Framework for Building AI Agents in 2026

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:

  • Whether you need multi-step workflows, memory or only model calls with tools.
  • How runs are traced and evaluated.
  • How tightly the framework is tied to one model provider.
  • Where state, queues and secrets live in each environment.

TypeScript agent frameworks at a glance

OptionApproachBest fit
AI SDKModel calls, streaming and tools across providersMost TypeScript agents, as a base layer
MastraAgents, workflows, memory and evalsTeams that want a full agent framework
LangGraph.jsAgents as stateful graphs with checkpointsBranching flows with human approval
OpenAI Agents SDKAgents with handoffs, guardrails and tracingSeveral cooperating agents on OpenAI models
Compare features across all four options
CapabilityAI SDKMastraLangGraph.jsOpenAI Agents SDK
RoleModel toolkitAgent frameworkAgent orchestrationAgent framework
StateYou store itMemory with storage adaptersCheckpointersSessions
Background runsYou add a queueWorkflowsYou add a queueYou add a queue
SecretsEnvironment variablesEnvironment variablesEnvironment variablesEnvironment variables
Production infrastructureYou provision itYou provision itYou provision itYou provision it

AI SDK: the base layer

AI SDK

The AI SDK offers generateText, streamText and tool definitions with Zod schemas across many providers, plus UI hooks for streaming chat.

Key features

  • One interface across model providers.
  • Tools with typed arguments and multi-step tool calling.
  • Streaming helpers for server and client.

Good to know

It is a toolkit rather than a framework, so memory, workflows and persistence are yours to build.

Try AI SDK →

Mastra: a full agent framework

Mastra

Mastra adds agents, workflows, memory, RAG and evals on top of the AI SDK, with a local playground for running agents.

Key features

  • Agents with tools and memory.
  • Workflows with steps, branching and suspend and resume.
  • Evals and tracing.

Good to know

Mastra makes many decisions for you, and its memory and workflow state need a storage backend in each environment.

Try Mastra →

LangGraph.js: graphs and checkpoints

LangGraph.js

LangGraph.js is the TypeScript version of LangGraph, modeling agents as graphs with shared state and checkpointers.

Key features

  • Explicit loops, branches and retries.
  • Checkpointing for resumable runs.
  • Interrupts for human approval.

Good to know

The graph model is more to learn than a tool loop, and the checkpoint store is another database to run.

Try LangGraph.js →

OpenAI Agents SDK: handoffs

OpenAI Agents SDK

The OpenAI Agents SDK for TypeScript provides agents, tools, handoffs, guardrails and tracing.

Key features

  • Handoffs between specialized agents.
  • Guardrails on inputs and outputs.
  • Built-in tracing.

Good to know

It is built around OpenAI's APIs first, and its default tracing is sent to OpenAI.

Try OpenAI Agents SDK →

Encore: infrastructure for the agent's backend

Encore

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 }; }, );
The endpoint returns a run id at once, and the subscription runs the agent with retries.

Key features

  • Pub/Sub subscriptions retry a run when a model call fails.
  • The API key is an Encore secret, set per environment instead of committed in a file.
  • Tracing shows the request, the published message and each query of a run.
  • When you ask a coding agent for a document upload feature, it can declare a bucket next to the topic, and encore run serves it locally right away.

Good to know

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.

Try Encore →

How to choose

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.

  • AI SDK as the base for model calls and tools.
  • Mastra for a full framework with workflows and memory.
  • LangGraph.js for graphs with checkpoints.
  • OpenAI Agents SDK for handoffs on OpenAI models.

Frequently asked questions

What is the best TypeScript framework for building AI agents?

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.

Should an AI agent run inside a request handler?

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.

How do I run an agent in the background with Encore?

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.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders