!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Get started](/v0.1/docs/get_started/)
    
    *   [Introduction](/v0.1/docs/get_started/introduction/)
    *   [Installation](/v0.1/docs/get_started/installation/)
    *   [Quickstart](/v0.1/docs/get_started/quickstart/)
*   [LangChain Expression Language](/v0.1/docs/expression_language/)
    
    *   [Get started](/v0.1/docs/expression_language/get_started/)
    *   [Why use LCEL?](/v0.1/docs/expression_language/why/)
    *   [Interface](/v0.1/docs/expression_language/interface/)
    *   [Streaming](/v0.1/docs/expression_language/streaming/)
    *   [How to](/v0.1/docs/expression_language/how_to/routing/)
        
    *   [Cookbook](/v0.1/docs/expression_language/cookbook/)
        
    *   [LangChain Expression Language (LCEL)](/v0.1/docs/expression_language/)
*   [Modules](/v0.1/docs/modules/)
    
    *   [Model I/O](/v0.1/docs/modules/model_io/)
        
    *   [Retrieval](/v0.1/docs/modules/data_connection/)
        
    *   [Chains](/v0.1/docs/modules/chains/)
    *   [Agents](/v0.1/docs/modules/agents/)
        
    *   [More](/v0.1/docs/modules/memory/)
        
*   [Security](/v0.1/docs/security/)
*   [Guides](/v0.1/docs/guides/)
    
    *   [Debugging](/v0.1/docs/guides/debugging/)
    *   [Deployment](/v0.1/docs/guides/deployment/)
        
    *   [Evaluation](/v0.1/docs/guides/evaluation/)
        
        *   [String Evaluators](/v0.1/docs/guides/evaluation/string/)
            
        *   [Comparison Evaluators](/v0.1/docs/guides/evaluation/comparison/)
            
        *   [Trajectory Evaluators](/v0.1/docs/guides/evaluation/trajectory/)
            
            *   [Agent Trajectory](/v0.1/docs/guides/evaluation/trajectory/trajectory_eval/)
        *   [Examples](/v0.1/docs/guides/evaluation/examples/)
            
    *   [Extending LangChain.js](/v0.1/docs/guides/extending_langchain/)
    *   [Fallbacks](/v0.1/docs/guides/fallbacks/)
    *   [LangSmith Walkthrough](/v0.1/docs/guides/langsmith_evaluation/)
    *   [Migrating to 0.1](/v0.1/docs/guides/migrating/)
*   [Ecosystem](/v0.1/docs/ecosystem/)
    
*   [LangGraph](/v0.1/docs/langgraph/)
*   * * *
    

*   [](/v0.1/)
*   [Guides](/v0.1/docs/guides/)
*   [Evaluation](/v0.1/docs/guides/evaluation/)
*   [Trajectory Evaluators](/v0.1/docs/guides/evaluation/trajectory/)
*   Agent Trajectory

Agent Trajectory
================

Agents can be difficult to holistically evaluate due to the breadth of actions and generation they can make. We recommend using multiple evaluation techniques appropriate to your use case. One way to evaluate an agent is to look at the whole trajectory of actions taken along with their responses.

Evaluators that do this can implement the `AgentTrajectoryEvaluator` interface. This walkthrough will show how to use the `trajectory` evaluator to grade an agent.

Methods[​](#methods "Direct link to Methods")
---------------------------------------------

The Agent Trajectory Evaluators are used with the \[evaluateAgentTrajectory\] method, which accept:

*   input (string) – The input to the agent.
*   prediction (string) – The final predicted response.
*   agentTrajectory (AgentStep\[\]) – The intermediate steps forming the agent trajectory

They return a dictionary with the following values:

*   score: Float from 0 to 1, where 1 would mean "most effective" and 0 would mean "least effective"
*   reasoning: String "chain of thought reasoning" from the LLM generated prior to creating the score

Usage[​](#usage "Direct link to Usage")
---------------------------------------

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/openai

    yarn add @langchain/openai

    pnpm add @langchain/openai

    import { OpenAI } from "@langchain/openai";import { Calculator } from "@langchain/community/tools/calculator";import { initializeAgentExecutorWithOptions } from "langchain/agents";import { loadEvaluator } from "langchain/evaluation";import { SerpAPI } from "@langchain/community/tools/serpapi";// Capturing Trajectory// The easiest way to return an agent's trajectory (without using tracing callbacks like those in LangSmith)// for evaluation is to initialize the agent with return_intermediate_steps=True.// Below, create an example agent we will call to evaluate.const model = new OpenAI({ temperature: 0 }, { baseURL: process.env.BASE_URL });const tools = [  new SerpAPI(process.env.SERPAPI_API_KEY, {    location: "Austin,Texas,United States",    hl: "en",    gl: "us",  }),  new Calculator(),];const executor = await initializeAgentExecutorWithOptions(tools, model, {  agentType: "zero-shot-react-description",  returnIntermediateSteps: true,});const input = `Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?`;const result = await executor.invoke({ input });// Evaluate Trajectoryconst chain = await loadEvaluator("trajectory");const res = await chain.evaluateAgentTrajectory({  prediction: result.output,  input,  agentTrajectory: result.intermediateSteps,});console.log({ res });/*{  res: {    reasoning: "i. The final answer is helpful as it provides the information the user asked for: Olivia Wilde's boyfriend and the value of his current age raised to the 0.23 power.\n" +      '\n' +      "ii. The AI language model uses a logical sequence of tools to answer the question. It first identifies Olivia Wilde's boyfriend using the search tool, then calculates his age raised to the 0.23 power using the calculator tool.\n" +      '\n' +      "iii. The AI language model uses the tools in a helpful way. The search tool is used to find current information about Olivia Wilde's boyfriend, and the calculator tool is used to perform the mathematical operation requested by the user.\n" +      '\n' +      'iv. The AI language model does not use too many steps to answer the question. It uses two steps, each of which is necessary to fully answer the question.\n' +      '\n' +      'v. The appropriate tools are used to answer the question. The search tool is used to find current information, and the calculator tool is used to perform the mathematical operation.\n' +      '\n' +      "However, there is a mistake in the calculation. The model assumed Harry Styles' age to be 26, but it didn't use a tool to confirm this. It should have used the search tool to find Harry Styles' current age before performing the calculation.\n" +      '\n' +      "Given these considerations, the model's performance can be rated as 3 out of 5.",    score: 0.5  }} */// Providing List of Valid Tools// By default, the evaluator doesn't take into account the tools the agent is permitted to call.// You can provide these to the evaluator via the agent_tools argument.const chainWithTools = await loadEvaluator("trajectory", { agentTools: tools });const res2 = await chainWithTools.evaluateAgentTrajectory({  prediction: result.output,  input,  agentTrajectory: result.intermediateSteps,});console.log({ res2 });/*{  res2: {    reasoning: "i. The final answer is helpful. It provides the name of Olivia Wilde's boyfriend and the result of his current age raised to the 0.23 power.\n" +      '\n' +      "ii. The AI language model uses a logical sequence of tools to answer the question. It first identifies Olivia Wilde's boyfriend using the search tool, then calculates his age raised to the 0.23 power using the calculator tool.\n" +      '\n' +      "iii. The AI language model uses the tools in a helpful way. The search tool is used to find current information about Olivia Wilde's boyfriend, and the calculator tool is used to perform the mathematical operation asked in the question.\n" +      '\n' +      'iv. The AI language model does not use too many steps to answer the question. It uses two steps, each corresponding to a part of the question.\n' +      '\n' +      'v. The appropriate tools are used to answer the question. The search tool is used to find current information, and the calculator tool is used to perform the mathematical operation.\n' +      '\n' +      "However, there is a mistake in the model's response. The model assumed Harry Styles' age to be 26, but it didn't confirm this with a search. This could lead to an incorrect calculation if his age is not 26.\n" +      '\n' +      "Given these considerations, I would give the model a score of 4 out of 5. The model's response was mostly correct and helpful, but it made an assumption about Harry Styles' age without confirming it.",    score: 0.75  }} */

#### API Reference:

*   [OpenAI](https://api.js.langchain.com/classes/langchain_openai.OpenAI.html) from `@langchain/openai`
*   [Calculator](https://api.js.langchain.com/classes/langchain_community_tools_calculator.Calculator.html) from `@langchain/community/tools/calculator`
*   [initializeAgentExecutorWithOptions](https://api.js.langchain.com/functions/langchain_agents.initializeAgentExecutorWithOptions.html) from `langchain/agents`
*   [loadEvaluator](https://api.js.langchain.com/functions/langchain_evaluation.loadEvaluator.html) from `langchain/evaluation`
*   [SerpAPI](https://api.js.langchain.com/classes/langchain_community_tools_serpapi.SerpAPI.html) from `@langchain/community/tools/serpapi`

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Trajectory Evaluators

](/v0.1/docs/guides/evaluation/trajectory/)[

Next

Examples

](/v0.1/docs/guides/evaluation/examples/)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.