LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangChain
  • Universal
  • Hub
  • Node
  • Load
  • Serializable
  • Encoder Backed
  • File System
  • In Memory
LangChain Core
  • Agents
  • Caches
  • Base
  • Dispatch
  • Web
  • Manager
  • Promises
  • Chat History
  • Context
  • Base
  • Langsmith
  • Documents
  • Embeddings
  • Errors
  • Example Selectors
  • Indexing
  • Base
  • Chat Models
  • Llms
  • Profile
  • Load
  • Serializable
  • Memory
  • Messages
  • Tool
  • Output Parsers
  • Openai Functions
  • Openai Tools
  • Outputs
  • Prompt Values
  • Prompts
  • Retrievers
  • Document Compressors
  • Runnables
  • Graph
  • Singletons
  • Stores
  • Structured Query
  • Tools
  • Base
  • Console
  • Log Stream
  • Run Collector
  • Tracer Langchain
  • Stream
  • Async Caller
  • Chunk Array
  • Context
  • Env
  • Event Source Parse
  • Format
  • Function Calling
  • Hash
  • Json Patch
  • Json Schema
  • Math
  • Ssrf
  • Stream
  • Testing
  • Tiktoken
  • Types
  • Vectorstores
Text Splitters
MCP Adapters
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

LangChain
UniversalHubNodeLoadSerializableEncoder BackedFile SystemIn Memory
LangChain Core
AgentsCachesBaseDispatchWebManagerPromisesChat HistoryContextBaseLangsmithDocumentsEmbeddingsErrorsExample SelectorsIndexingBaseChat ModelsLlmsProfileLoadSerializableMemoryMessagesToolOutput ParsersOpenai FunctionsOpenai ToolsOutputsPrompt ValuesPromptsRetrieversDocument CompressorsRunnablesGraphSingletonsStoresStructured QueryToolsBaseConsoleLog StreamRun CollectorTracer LangchainStreamAsync CallerChunk ArrayContextEnvEvent Source ParseFormatFunction CallingHashJson PatchJson SchemaMathSsrfStreamTestingTiktokenTypesVectorstores
Text Splitters
MCP Adapters
Language
Theme
JavaScript@langchain/coreutilscontextcontext
Functionā—Since v1.1

context

A tagged template function for creating formatted strings.

This utility provides a clean, template literal-based API for string formatting that can be used for prompts, descriptions, and other text formatting needs.

It automatically handles whitespace normalization and indentation, making it ideal for multi-line strings in code.

When using this utility, it will:

  • Strip common leading indentation from all lines
  • Trim leading/trailing whitespace
  • Align multi-line interpolated values to match indentation
  • Support escape sequences: \\n (newline), \\`` (backtick), \$(dollar),{` (brace)
Copy
context(strings: TemplateStringsArray, values: unknown[]): string

Used in Docs

  • Build a SQL assistant with on-demand skills

Parameters

NameTypeDescription
strings*TemplateStringsArray
values*unknown[]

Example 1

Copy
import { context } from "@langchain/core/utils/context";

const role = "agent";
const prompt = context`
  You are an ${role}.
  Your task is to help users.
`;
// Returns: "You are an agent.\nYour task is to help users."

Example 2

Copy
// Multi-line interpolated values are aligned
const items = "- Item 1\n- Item 2\n- Item 3";
const message = context`
  Shopping list:
    ${items}
  End of list.
`;
// The items will be indented to match "    " (4 spaces)
View source on GitHub