Building with LangChain

He understands the chains and what happens between a model decision and a tool's execution.

LangChain offers reusable parts to connect models and tools. Their value is understood by following the messages that travel between your application, model and the functions that run actions.

Key concepts

LangChain is an application framework with LLM. Unifies some of the interplay with providers and allows to compose prompts, models, tools and results processing. LangGraph provides Flow Control and state for agents LangSmith It serves as an observation and appraisal. Learning the basic level doesn't require simultaneously mastering the whole ecosystem.

One Chain Connects steps defined by the developer. An agent lets the model propose tools and order from execution within boundaries. Sharing an interface between models facilitates changes but does not guarantee identical capabilities: verifies tool calling, parameters, boundaries and performance of the chosen supplier.

Module Objectives

Reusable blocks

A prompt dose It combines constant instructions with variables. "Explains {concepto} with an example" separates the structure from the topic. A prompt chat preserves roles: system instructions, user messages and earlier responses. A few-shot example illustrates the expected format. remains different from an authorisation rule.

A composition as prompt | model | parser describes a sequence using LCEL, LangChain Expression Language. The prompt prepares the entrance, the model produces an answer and a parser removes or valids the output form. A text parser doesn't check that statements are true: format and veracity are separate problems.

The tools are usually expressed as functions with types and descriptions. The decorator @tool allows an exposure of metadata to the framework. create_agent brings together the model and tools, and agent.invoke start the execution. That simplicity of using hidden useful work does not remove security responsibilities. The Current documentation of agents explain this construction and its configuration.

What's going on under the hood

The runtime prepares messages and tool schemes. The model can return text, calls to tools or other supported blocks; Don't figure it's finished just because text came up. When there's an outstanding call, the application identifies the registered name, analyzes arguments and runs the allowed tool.

Each result is linked with the appropriate call identifier. It then goes into the context of the following request. Thus the model can use the obtained value, demand another operation or produce a final response. The loop should also have boundaries and manage errors: to finish doesn't depend exclusively on how the model decides to do so.

Retain state does not mean that the LLM modifies its weights or permanently learns its user name. The application or a service has a context and contributes to future interference. Sending the entire verbatim history over the network is not required in every integration either: continuation, trimming and summarization mechanisms may be available. What's necessary is to preserve the relevant context and relationship between calls and results.

Step-by-step example

Consider "18 Multiples by 5 and divide the result between 3." The tools available are multiplicar y dividir.

Pass Responsible Evidence observable
1 Application Add the question to the messages and call agent.invoke.
2 Model Requests multiplicar(18, 5) with an identifier call-a.
3 Runtime Executs the function register 90 for call-a.
4 Model Requests dividir(90, 3) how call-b.
5 Runtime Valid divisor other than zero; come back 30.
6 Model and run time They produce and deliver the final answer: 30.

The multiplication function does not call the LLM. It's regular code inside an application that does use a model. To check the demonstration, check both the result and the arguments: get 30 by chance doesn't prove that the right tools have been used.

Test after "divide between zero." The expected result is a controlled error or a correction request, not an exception without handling an invented number. Finally, it deliberately removes the result of call-a and explain why we lose the evidence that the second operation required.

Common errors

Retirement exercise

The multiplication returns 90 correctly, but the agent rerequests it several times. What two points would you review before you increased the step limit?

Solution

First, confirm that the result appears in the context with the right call identifier. Second, check instructions, tool description and completion conditions to check that the agent recognizes the result. To increase the limit could make more face a defective execution without redressing the cause. It also adds a limit for the failure to remain controllable.

Sources and review

Query Agents, Tools y Messages LangChain's official documentation. The key learning is to be able to figure out, in every arrow of the diagram, what message circulates, who interprets and where the code is run.

Search at CertiTips

12 available courses.