agentleFS
yevhens-hueyevhens-hue/claude-skills-starter-kit/skills/langchain-agent-patterns/SKILL.md

langchain-agent-patterns

Skill4 starsChanged 3 months agoMITView on GitHub

Production LangChain LCEL & Agent Architecture. Covers LangChain Expression Language (LCEL) piping, @tool docstrings & type hint schema generation, output parsers (StrOutputParser, JsonOutputParser), agent state management, and agent.invoke() ReAct execution loops.

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    When calling agent.invoke(), LangChain automatically handles:

    line 77

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

🦜🔗 LangChain LCEL & Agent Architecture Patterns

LangChain provides composable primitives for building LLM pipelines via LangChain Expression Language (LCEL) and orchestrating tool-calling agents.


1. LangChain Expression Language (LCEL)

LCEL uses the pipe operator (|) to compose components into declarative, streamable chains.

$$\text{PromptTemplate} \xrightarrow{\quad\vert\quad} \text{ChatModel} \xrightarrow{\quad\vert\quad} \text{OutputParser}$$

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI

# 1. Define Prompt Template
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are an expert software architect. Provide concise code reviews."),
    ("user", "{code_snippet}")
])

# 2. Define Model
model = ChatOpenAI(model="gpt-4o-mini", temperature=0.0)

# 3. Compose Chain via LCEL Pipe
review_chain = prompt | model | StrOutputParser()

# 4. Invoke Chain
result = review_chain.invoke({"code_snippet": "def add(a, b): return a + b"})
print(result)

2. Defining Tools (@tool)

In LangChain, docstrings are mandatory because they are passed directly to the LLM as the tool description. Type hints are used to build the JSON input schema.

from langchain_core.tools import tool
from pydantic import BaseModel, Field

# Option A: Functional @tool with Type Hints & Docstring
@tool
def calculate_tax(amount: float, tax_rate: float = 0.20) -> float:
    """
    Calculates tax amount for a given financial transaction value.
    
    Args:
        amount: The total monetary amount.
        tax_rate: Tax percentage in decimal format (default: 0.20 for 20%).
    """
    return amount * tax_rate

# Option B: Pydantic Schema for Complex Tools
class SearchInput(BaseModel):
    query: str = Field(description="Search keywords or domain query")
    max_results: int = Field(default=5, description="Number of results to return")

@tool("domain_search", args_schema=SearchInput)
def domain_search(query: str, max_results: int = 5) -> str:
    """Searches the internal knowledge database for matched records."""
    return f"Found {max_results} matches for '{query}'"

3. Agent Execution (agent.invoke())

When calling agent.invoke(), LangChain automatically handles:

  1. Converting tool definitions to LLM tool schemas.
  2. Intercepting tool-call requests from the model.
  3. Executing local tool functions with parsed parameters.
  4. Feeding observations back into the model in a ReAct loop.
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate

tools = [calculate_tax, domain_search]

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful financial assistant."),
    ("placeholder", "{chat_history}"),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

# Create Agent & Agent Executor
agent = create_tool_calling_agent(model, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# Execute ReAct Agent Loop
response = agent_executor.invoke({"input": "What is the 20% tax on $1,500?"})
print(response["output"])

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.