Comprehensive TypeScript framework for building LLM-powered applications with agents, middleware, and tool orchestration.
npx @tessl/cli install tessl/npm-langchain@1.1.0TypeScript framework for building LLM-powered applications with agents, middleware, tools, and multi-provider model support.
langchainnpm install langchainimport {
createAgent,
initChatModel,
tool,
AIMessage,
HumanMessage,
SystemMessage
} from "langchain";import { createAgent, tool } from "langchain";
import { z } from "zod";
const weatherTool = tool(
({ location }) => `Weather in ${location}: sunny`,
{
name: "get_weather",
description: "Get weather for a location",
schema: z.object({
location: z.string().describe("City name"),
}),
}
);
const agent = createAgent({
model: "openai:gpt-4o",
tools: [weatherTool],
systemPrompt: "You are a weather assistant.",
});
const result = await agent.invoke({
messages: [{ role: "user", content: "What's the weather in NYC?" }],
});// Messages
type BaseMessage = any;
type AIMessage = any;
type HumanMessage = any;
type SystemMessage = any;
type ToolMessage = any;
type BaseMessageChunk = any;
// Models
type BaseChatModel = any;
type LanguageModelLike = BaseChatModel;
type BaseLanguageModelInput = string | BaseMessage[];
interface ConfigurableChatModelCallOptions {
tools?: (StructuredToolInterface | Record<string, unknown> | ToolDefinition | RunnableToolLike)[];
[key: string]: any;
}
// Tools
type ServerTool = StructuredTool | DynamicStructuredTool | Tool | DynamicTool;
interface ClientTool {
name: string;
description: string;
schema: InteropZodType;
}
interface ToolCall {
id: string;
name: string;
args: Record<string, any>;
}
// Zod Types
type InteropZodType<T = any> = any;
type InteropZodObject = any;
type InteropZodDefault<T> = any;
type InteropZodOptional<T> = any;
type InferInteropZodInput<T extends InteropZodType> = any;
// State
type AnyAnnotationRoot = any;
type BaseCheckpointSaver = any;
interface BaseStore<K = string, V = any> {
mget(keys: K[]): Promise<(V | undefined)[]>;
mset(keyValuePairs: [K, V][]): Promise<void>;
mdelete(keys: K[]): Promise<void>;
yieldKeys(prefix?: string): AsyncGenerator<K>;
}
interface Interrupt<TValue = unknown> {
id: string;
value: TValue;
}
// Configuration
interface RunnableConfig {
signal?: AbortSignal;
callbacks?: any;
tags?: string[];
metadata?: Record<string, any>;
runName?: string;
}
type Callbacks = any;
// Documents
interface DocumentInput<Metadata extends Record<string, any> = Record<string, any>> {
pageContent: string;
metadata?: Metadata;
}
class Document<Metadata extends Record<string, any> = Record<string, any>> {
pageContent: string;
metadata: Metadata;
constructor(fields: DocumentInput<Metadata>);
}Agent System - Full agent API documentation
function createAgent<
T extends Record<string, any> = Record<string, any>,
StateSchema extends AnyAnnotationRoot | InteropZodObject | undefined = undefined,
ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot,
TMiddleware extends readonly AgentMiddleware[] = readonly AgentMiddleware[]
>(
params: CreateAgentParams<T, StateSchema, ContextSchema>
): ReactAgent<T, StateSchema, ContextSchema, TMiddleware>;
interface CreateAgentParams<
StructuredResponseType extends Record<string, any> = Record<string, any>,
StateSchema extends AnyAnnotationRoot | InteropZodObject | undefined = undefined,
ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot
> {
model: string | LanguageModelLike;
tools?: (ServerTool | ClientTool)[];
systemPrompt?: string | SystemMessage;
stateSchema?: StateSchema;
contextSchema?: ContextSchema;
checkpointer?: BaseCheckpointSaver | boolean;
store?: BaseStore;
responseFormat?: InteropZodType<StructuredResponseType> | JsonSchemaFormat | ResponseFormat;
middleware?: readonly AgentMiddleware[];
name?: string;
description?: string;
includeAgentName?: "inline" | undefined;
signal?: AbortSignal;
version?: "v1" | "v2";
}class ReactAgent<
StructuredResponseFormat extends Record<string, any> | ResponseFormatUndefined = Record<string, any>,
StateSchema extends AnyAnnotationRoot | InteropZodObject | undefined = undefined,
ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot,
TMiddleware extends readonly AgentMiddleware[] = readonly AgentMiddleware[]
> {
invoke(
state: UserInput<StateSchema> & InferMiddlewareInputStates<TMiddleware>,
config?: InvokeConfiguration<InferContextInput<ContextSchema> & InferMiddlewareContextInputs<TMiddleware>>
): Promise<MergedAgentState<StateSchema, StructuredResponseFormat, TMiddleware>>;
stream<TStreamMode extends StreamMode | StreamMode[] | undefined>(
state: UserInput<StateSchema> & InferMiddlewareInputStates<TMiddleware>,
config?: StreamConfiguration<InferContextInput<ContextSchema> & InferMiddlewareContextInputs<TMiddleware>, TStreamMode>
): Promise<IterableReadableStream<StreamOutputMap<TStreamMode, ...>>>;
}
interface UserInput<TStateSchema extends AnyAnnotationRoot | InteropZodObject | undefined = undefined> {
messages: Array<BaseMessage | { role: string; content: string }>;
}
interface BuiltInState {
messages: BaseMessage[];
__interrupt__?: Interrupt[];
jumpTo?: JumpToTarget;
}
type JumpToTarget = "model_request" | "tools";
type StreamMode = "values" | "updates" | "messages" | "debug";Middleware - Full middleware API documentation
function createMiddleware<
TSchema extends InteropZodObject | undefined = undefined,
TContextSchema extends InteropZodObject | InteropZodDefault<InteropZodObject> | InteropZodOptional<InteropZodObject> | undefined = undefined
>(
middleware: AgentMiddleware<TSchema, TContextSchema>
): AgentMiddleware<TSchema, TContextSchema>;
interface AgentMiddleware<TSchema = any, TContextSchema = any, TFullContext = any> {
name: string;
stateSchema?: TSchema;
contextSchema?: TContextSchema;
tools?: (ClientTool | ServerTool)[];
wrapToolCall?: WrapToolCallHook<TSchema, TFullContext>;
wrapModelCall?: WrapModelCallHook<TSchema, TFullContext>;
beforeAgent?: BeforeAgentHook<TSchema, TFullContext>;
beforeModel?: BeforeModelHook<TSchema, TFullContext>;
afterModel?: AfterModelHook<TSchema, TFullContext>;
afterAgent?: AfterAgentHook<TSchema, TFullContext>;
}
interface Runtime<TContext = unknown> {
metadata: Record<string, any>;
signal: AbortSignal;
context: TContext;
interrupt: (value: any) => void;
configurable?: {
thread_id?: string;
[key: string]: unknown;
};
}function humanInTheLoopMiddleware(config: HumanInTheLoopMiddlewareConfig): AgentMiddleware;
function summarizationMiddleware(config: SummarizationMiddlewareConfig): AgentMiddleware;
function dynamicSystemPromptMiddleware(fn: (state: AgentBuiltInState, runtime: Runtime) => string | SystemMessage | Promise<string | SystemMessage>): AgentMiddleware;
function llmToolSelectorMiddleware(config: LLMToolSelectorConfig): AgentMiddleware;
function piiMiddleware(piiType: BuiltInPIIType | string, options?: PIIMiddlewareOptions): AgentMiddleware;
function piiRedactionMiddleware(config: PIIRedactionMiddlewareConfig): AgentMiddleware;
function contextEditingMiddleware(config: ContextEditingMiddlewareConfig): AgentMiddleware;
function toolCallLimitMiddleware(config: ToolCallLimitConfig): AgentMiddleware;
function todoListMiddleware(options?: TodoListMiddlewareOptions): AgentMiddleware;
function modelCallLimitMiddleware(config: ModelCallLimitMiddlewareConfig): AgentMiddleware;
function modelFallbackMiddleware(...fallbackModels: (string | LanguageModelLike)[]): AgentMiddleware;
function modelRetryMiddleware(config?: ModelRetryMiddlewareConfig): AgentMiddleware;
function toolRetryMiddleware(config?: ToolRetryMiddlewareConfig): AgentMiddleware;
function toolEmulatorMiddleware(options: ToolEmulatorOptions): AgentMiddleware;
function openAIModerationMiddleware(options?: OpenAIModerationMiddlewareOptions): AgentMiddleware;
function anthropicPromptCachingMiddleware(config?: PromptCachingMiddlewareConfig): AgentMiddleware;
function countTokensApproximately(messages: BaseMessage[]): number;Chat Models - Full chat model API documentation
async function initChatModel<
RunInput extends BaseLanguageModelInput = BaseLanguageModelInput,
CallOptions extends ConfigurableChatModelCallOptions = ConfigurableChatModelCallOptions
>(
model?: string,
fields?: InitChatModelFields
): Promise<ConfigurableModel<RunInput, CallOptions>>;
interface InitChatModelFields {
modelProvider?: ChatModelProvider;
configurableFields?: string[] | "any";
configPrefix?: string;
profile?: ModelProfile;
[key: string]: any;
}
type ChatModelProvider =
| "openai" | "anthropic" | "azure_openai" | "cohere"
| "google-vertexai" | "google-vertexai-web" | "google-genai" | "ollama"
| "mistralai" | "mistral" | "groq" | "bedrock" | "deepseek"
| "xai" | "cerebras" | "fireworks" | "together" | "perplexity";Tools - Full tools API documentation
function tool<T extends InteropZodType = InteropZodType>(
func: (input: InferInteropZodInput<T>, config?: RunnableConfig) => any,
config: {
name: string;
description: string;
schema: T;
}
): DynamicStructuredTool<T>;
class DynamicStructuredTool<T extends InteropZodType = InteropZodType> extends StructuredTool<T> {
name: string;
description: string;
schema: T;
invoke(input: InferInteropZodInput<T>, config?: RunnableConfig): Promise<any>;
}
abstract class StructuredTool<T extends InteropZodType = InteropZodType> extends Tool {
schema: T;
abstract _call(arg: InferInteropZodInput<T>, runManager?: CallbackManagerForToolRun): Promise<string>;
}Hub - LangChain Hub integration
function pull<T extends Runnable>(ownerRepoCommit: string): Promise<T>;
function push(
repoFullName: string,
runnable: Runnable,
options?: {
apiUrl?: string;
apiKey?: string;
parentCommitHash?: string;
isPublic?: boolean;
description?: string;
readme?: string;
tags?: string[];
}
): Promise<string>;Storage - Persistent storage implementations
class InMemoryStore<T = any> implements BaseStore<string, T> {
mget(keys: string[]): Promise<(T | undefined)[]>;
mset(keyValuePairs: [string, T][]): Promise<void>;
mdelete(keys: string[]): Promise<void>;
yieldKeys(prefix?: string): AsyncGenerator<string>;
}function filterMessages(
messages: BaseMessage[],
options: FilterMessagesOptions
): BaseMessage[];
function trimMessages(
messages: BaseMessage[],
options: TrimMessagesOptions
): BaseMessage[];
interface FilterMessagesOptions {
includeTypes?: string[];
excludeTypes?: string[];
includeIds?: string[];
excludeIds?: string[];
includeNames?: string[];
excludeNames?: string[];
}
interface TrimMessagesOptions {
maxTokens?: number;
tokenCounter?: (messages: BaseMessage[]) => number | Promise<number>;
strategy?: "first" | "last";
allowPartial?: boolean;
startOn?: string | string[];
endOn?: string | string[];
includeSystem?: boolean;
}class MultipleToolsBoundError extends Error {}
class MultipleStructuredOutputsError extends Error {}
class StructuredOutputParsingError extends Error {}
class ToolInvocationError extends Error {}