{"slug":"ai-orchestration-langchain","title":"ai-orchestration-langchain","summary":"LangChain.js patterns for building LLM applications — chat models, LCEL chains, prompt templates, structured output, agents, tools, RAG, streaming, and LangSmith tracing","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-09-29T15:27:50.274803Z","repo":{"url":"https://github.com/agents-inc/skills","stars":24,"forks":8,"license":"MIT","updatedAt":"2026-09-07T17:50:55Z"},"bodyHtml":"<hr>\n<h2>name: ai-orchestration-langchain\ndescription: LangChain.js patterns for building LLM applications — chat models, LCEL chains, prompt templates, structured output, agents, tools, RAG, streaming, and LangSmith tracing</h2>\n<h1>LangChain.js Patterns</h1>\n<blockquote>\n<p><strong>Quick Guide:</strong> Use LangChain.js (v1.x) to build composable LLM applications. Use LCEL (<code>prompt.pipe(model).pipe(parser)</code>) for all chain composition -- never use legacy <code>LLMChain</code>. Use <code>withStructuredOutput(zodSchema)</code> for typed responses. Use <code>createAgent()</code> (LangGraph-backed) for agentic workflows -- <code>AgentExecutor</code> is legacy. All <code>@langchain/*</code> packages must share the same <code>@langchain/core</code> version or you get cryptic type errors at runtime.</p>\n</blockquote>\n<hr>\n<p>&lt;critical_requirements&gt;</p>\n<h2>CRITICAL: Before Using This Skill</h2>\n<blockquote>\n<p><strong>All code must follow project conventions in CLAUDE.md</strong> (kebab-case, named exports, import ordering, <code>import type</code>, named constants)</p>\n</blockquote>\n<p><strong>(You MUST use LCEL pipe composition (<code>prompt.pipe(model).pipe(parser)</code>) for all chains -- never use legacy <code>LLMChain</code>, <code>ConversationChain</code>, or <code>SequentialChain</code>)</strong></p>\n<p><strong>(You MUST ensure all <code>@langchain/*</code> packages depend on the same version of <code>@langchain/core</code> -- version mismatches cause cryptic runtime errors)</strong></p>\n<p><strong>(You MUST use <code>withStructuredOutput(zodSchema)</code> for structured LLM responses -- never manually parse JSON from completion text)</strong></p>\n<p><strong>(You MUST use <code>createAgent()</code> from <code>langchain</code> for new agent code -- <code>AgentExecutor</code> and <code>createToolCallingAgent</code> are legacy patterns)</strong></p>\n<p><strong>(You MUST never hardcode API keys -- use environment variables (<code>OPENAI_API_KEY</code>, <code>ANTHROPIC_API_KEY</code>, etc.))</strong></p>\n<p>&lt;/critical_requirements&gt;</p>\n<hr>\n<p><strong>Auto-detection:</strong> LangChain, langchain, @langchain/core, @langchain/openai, @langchain/anthropic, @langchain/google-genai, ChatOpenAI, ChatAnthropic, ChatPromptTemplate, StringOutputParser, RunnableSequence, pipe, withStructuredOutput, createAgent, createToolCallingAgent, AgentExecutor, tool, DynamicStructuredTool, RecursiveCharacterTextSplitter, MemoryVectorStore, OpenAIEmbeddings, LCEL, LangSmith, LANGCHAIN_TRACING_V2</p>\n<p><strong>When to use:</strong></p>\n<ul>\n<li>Building LLM applications that compose prompts, models, and output parsers into chains</li>\n<li>Creating agentic workflows where models decide which tools to call</li>\n<li>Implementing RAG pipelines with document loading, splitting, embedding, and retrieval</li>\n<li>Needing structured output from LLMs with type-safe Zod schema validation</li>\n<li>Streaming LLM responses token-by-token to users</li>\n<li>Switching between LLM providers (OpenAI, Anthropic, Google) with a unified interface</li>\n<li>Tracing and debugging LLM applications with LangSmith</li>\n</ul>\n<p><strong>Key patterns covered:</strong></p>\n<ul>\n<li>Chat model initialization and provider switching (ChatOpenAI, ChatAnthropic, ChatGoogleGenerativeAI)</li>\n<li>LCEL chain composition with <code>.pipe()</code> and <code>RunnableSequence</code></li>\n<li>Prompt templates (<code>ChatPromptTemplate</code>, <code>MessagesPlaceholder</code>)</li>\n<li>Structured output with <code>withStructuredOutput()</code> and Zod schemas</li>\n<li>Tool definition with <code>tool()</code> function and Zod schemas</li>\n<li>Agent creation with <code>createAgent()</code> (LangGraph-backed)</li>\n<li>RAG pipelines: document loaders, text splitters, vector stores, retrievers</li>\n<li>Streaming from chains, models, and agents</li>\n<li>LangSmith tracing setup</li>\n</ul>\n<p><strong>When NOT to use:</strong></p>\n<ul>\n<li>You only call one LLM provider and want the thinnest wrapper -- use the provider's SDK directly</li>\n<li>You need React-specific chat UI hooks (<code>useChat</code>, <code>useCompletion</code>) -- use a framework-integrated AI SDK</li>\n<li>You want a simple single-call completion with no chaining -- a direct SDK call is simpler</li>\n<li>You need real-time bidirectional communication -- LangChain does not cover WebSocket/Realtime APIs</li>\n</ul>\n<hr>\n<h2>Examples Index</h2>\n<ul>\n<li><a href=\"examples/core.md\">Core: Setup, LCEL &amp; Chat Models</a> -- Package installation, chat model init, LCEL chains, prompt templates, output parsers</li>\n<li><a href=\"examples/structured-output-tools.md\">Structured Output &amp; Tools</a> -- <code>withStructuredOutput</code>, tool definition, binding tools to models</li>\n<li><a href=\"examples/agents.md\">Agents</a> -- <code>createAgent</code>, tool-calling agents, chat history, streaming agents</li>\n<li><a href=\"examples/rag.md\">RAG Pipelines</a> -- Document loaders, text splitters, vector stores, retrieval chains</li>\n<li><a href=\"examples/streaming.md\">Streaming</a> -- Model streaming, chain streaming, agent streaming</li>\n<li><a href=\"reference.md\">Quick API Reference</a> -- Package map, import paths, environment variables, model IDs</li>\n</ul>\n<hr>\n\n<hr>\n\n<hr>\n<p>&lt;decision_framework&gt;</p>\n<h2>Decision Framework</h2>\n<h3>When to Use LangChain vs Direct SDK</h3>\n<pre><code>Do you need multi-step LLM workflows (prompt -&gt; model -&gt; parser -&gt; ...)?\n+-- YES -&gt; Use LangChain (LCEL chains)\n+-- NO -&gt; Do you need to swap between LLM providers?\n    +-- YES -&gt; Use LangChain (unified chat model interface)\n    +-- NO -&gt; Do you need RAG or agent tool calling?\n        +-- YES -&gt; Use LangChain\n        +-- NO -&gt; Use the provider SDK directly (simpler, fewer deps)\n</code></pre>\n<h3>Which Chat Model Class</h3>\n<pre><code>Which provider?\n+-- OpenAI -&gt; ChatOpenAI from @langchain/openai\n+-- Anthropic -&gt; ChatAnthropic from @langchain/anthropic\n+-- Google -&gt; ChatGoogleGenerativeAI from @langchain/google-genai\n+-- Runtime selection -&gt; initChatModel(\"provider:model\") from langchain\n+-- Other -&gt; Check @langchain/community\n</code></pre>\n<h3>LCEL vs createAgent</h3>\n<pre><code>Does the model need to autonomously decide when to call tools?\n+-- YES -&gt; createAgent() (handles tool-call loops, state management)\n+-- NO -&gt; Is it a fixed sequence of steps?\n    +-- YES -&gt; LCEL chain (prompt.pipe(model).pipe(parser))\n    +-- NO -&gt; RunnableSequence.from() with branching\n</code></pre>\n<h3>Legacy Chain vs LCEL</h3>\n<pre><code>Are you writing new code?\n+-- YES -&gt; ALWAYS use LCEL (.pipe()) -- never legacy chains\n+-- NO -&gt; Is the existing code using LLMChain/ConversationChain?\n    +-- YES -&gt; Migrate to LCEL when touching the code\n    +-- NO -&gt; Keep as-is if it works\n</code></pre>\n<p>&lt;/decision_framework&gt;</p>\n<hr>\n<p>&lt;red_flags&gt;</p>\n<h2>RED FLAGS</h2>\n<p><strong>High Priority Issues:</strong></p>\n<ul>\n<li>Using legacy chains (<code>LLMChain</code>, <code>ConversationChain</code>, <code>SequentialChain</code>) instead of LCEL -- these are deprecated</li>\n<li>Mismatched <code>@langchain/core</code> versions across packages -- causes <code>instanceof</code> checks to fail silently, methods to be undefined, and type errors</li>\n<li>Hardcoding API keys instead of using environment variables</li>\n<li>Manually parsing JSON from LLM text output instead of using <code>withStructuredOutput()</code></li>\n<li>Using <code>AgentExecutor</code> for new code instead of <code>createAgent()</code></li>\n</ul>\n<p><strong>Medium Priority Issues:</strong></p>\n<ul>\n<li>Using camelCase tool names (<code>getWeather</code>) instead of snake_case (<code>get_weather</code>) -- some providers reject camelCase</li>\n<li>Not adding <code>.describe()</code> to Zod schema fields for tools -- model gets no guidance on argument format</li>\n<li>Using <code>BufferMemory</code> / <code>ConversationSummaryMemory</code> -- these are deprecated, use LangGraph checkpointing or <code>RunnableWithMessageHistory</code></li>\n<li>Not setting <code>LANGCHAIN_CALLBACKS_BACKGROUND=true</code> in non-serverless environments -- adds latency to every LLM call when tracing is on</li>\n<li>Importing from <code>langchain/</code> (main package) when the import should come from <code>@langchain/core/</code> or a provider package</li>\n</ul>\n<p><strong>Common Mistakes:</strong></p>\n<ul>\n<li>Installing <code>langchain</code> without <code>@langchain/core</code> -- <code>@langchain/core</code> is a required peer dependency</li>\n<li>Mixing <code>@langchain/core</code> v0.x with <code>langchain</code> v1.x -- all packages must be on compatible versions</li>\n<li>Using <code>RunnableLambda</code> in a chain and expecting <code>.stream()</code> to work -- lambda functions do not propagate streaming by default; subclass <code>Runnable</code> and implement <code>transform</code> instead</li>\n<li>Forgetting that <code>ChatPromptTemplate.fromTemplate()</code> creates a single user message -- use <code>ChatPromptTemplate.fromMessages()</code> for multi-message prompts with system/assistant/user roles</li>\n<li>Using <code>MemoryVectorStore</code> in production -- it is in-memory only, all data is lost on restart; use a persistent vector store</li>\n</ul>\n<p><strong>Gotchas &amp; Edge Cases:</strong></p>\n<ul>\n<li><code>@langchain/core</code> is a peer dependency, not a transitive dependency. You must install it explicitly: <code>npm install @langchain/core</code>. If you see \"cannot resolve @langchain/core\" or <code>instanceof</code> checks failing, you likely have duplicate core versions -- run <code>npm ls @langchain/core</code> to check.</li>\n<li><code>withStructuredOutput()</code> uses function calling under the hood, not JSON mode. Not all models support it -- check provider docs. If the model does not support function calling, use <code>JsonOutputParser</code> with a prompt instead.</li>\n<li><code>ChatPromptTemplate.fromMessages()</code> uses tuple syntax <code>[\"system\", \"...\"]</code> or <code>[\"human\", \"...\"]</code> -- the role names are <code>system</code>, <code>human</code>, <code>ai</code>, not <code>developer</code>, <code>user</code>, <code>assistant</code>.</li>\n<li><code>tool()</code> from <code>@langchain/core/tools</code> vs <code>tool()</code> from <code>langchain</code> -- both exist. The <code>langchain</code> re-export is a convenience wrapper. Use whichever matches your import pattern but be consistent.</li>\n<li><code>initChatModel()</code> requires the provider package to be installed. If you call <code>initChatModel(\"anthropic:claude-sonnet-4-5-20250929\")</code> without <code>@langchain/anthropic</code> installed, you get a confusing module resolution error, not a clear \"package not installed\" message.</li>\n<li>Zod v4 works with <code>StateSchema</code> and <code>createAgent</code>, but <code>withStructuredOutput()</code> may have partial Zod v4 support -- test with your version and fall back to Zod v3.x if schema validation fails.</li>\n<li><code>RecursiveCharacterTextSplitter</code> now lives in <code>@langchain/textsplitters</code> (separate package), not <code>langchain/text_splitter</code>.</li>\n<li>When using streaming with <code>createAgent()</code>, use <code>streamMode: \"values\"</code> to get full state at each step, or omit for incremental updates.</li>\n</ul>\n<p>&lt;/red_flags&gt;</p>\n<hr>\n<p>&lt;critical_reminders&gt;</p>\n<h2>CRITICAL REMINDERS</h2>\n<blockquote>\n<p><strong>All code must follow project conventions in CLAUDE.md</strong> (kebab-case, named exports, import ordering, <code>import type</code>, named constants)</p>\n</blockquote>\n<p><strong>(You MUST use LCEL pipe composition (<code>prompt.pipe(model).pipe(parser)</code>) for all chains -- never use legacy <code>LLMChain</code>, <code>ConversationChain</code>, or <code>SequentialChain</code>)</strong></p>\n<p><strong>(You MUST ensure all <code>@langchain/*</code> packages depend on the same version of <code>@langchain/core</code> -- version mismatches cause cryptic runtime errors)</strong></p>\n<p><strong>(You MUST use <code>withStructuredOutput(zodSchema)</code> for structured LLM responses -- never manually parse JSON from completion text)</strong></p>\n<p><strong>(You MUST use <code>createAgent()</code> from <code>langchain</code> for new agent code -- <code>AgentExecutor</code> and <code>createToolCallingAgent</code> are legacy patterns)</strong></p>\n<p><strong>(You MUST never hardcode API keys -- use environment variables (<code>OPENAI_API_KEY</code>, <code>ANTHROPIC_API_KEY</code>, etc.))</strong></p>\n<p><strong>Failure to follow these rules will produce fragile, hard-to-debug LLM applications with version conflicts and untyped outputs.</strong></p>\n<p>&lt;/critical_reminders&gt;</p>\n","files":[{"path":"examples/agents.md","sizeBytes":5404,"isText":true},{"path":"examples/core.md","sizeBytes":6155,"isText":true},{"path":"examples/rag.md","sizeBytes":6523,"isText":true},{"path":"examples/streaming.md","sizeBytes":5653,"isText":true},{"path":"examples/structured-output-tools.md","sizeBytes":7272,"isText":true},{"path":"reference.md","sizeBytes":9571,"isText":true},{"path":"SKILL.md","sizeBytes":20903,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-09-29T15:28:01.171429Z","sha256":"370B9C40FC6BB754FB8A1DDA976BBDB5F219D06FD7FE3B4B49E4727A8662E7CC","sizeBytes":21345},"review":null,"source":{"repositoryUrl":"https://github.com/agents-inc/skills","path":"dist/plugins/ai-orchestration-langchain/skills/ai-orchestration-langchain","license":"MIT","commit":"3a51ef571e996b18294bf776d53dbdad26de0617","subtreeSha":"9560D9ADAC7F5F4288A0AE61EA20AF6F7EE8927C4A79ECBD86D5D4DD03391156","lastSyncedAt":"2026-09-29T15:27:48.914434Z"},"reviewedAt":"2026-09-29T15:29:06.815328Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/agents-inc/skills/tree/main/dist/plugins/ai-orchestration-langchain/skills/ai-orchestration-langchain"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install agents-inc-skills@llmmart"},{"target":"git","command":"git clone https://github.com/agents-inc/skills.git"}]}