!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/)
        
        *   [Quick start](/v0.1/docs/modules/agents/quick_start/)
        *   [Concepts](/v0.1/docs/modules/agents/concepts/)
        *   [Agent Types](/v0.1/docs/modules/agents/agent_types/)
            
        *   [How-to](/v0.1/docs/modules/agents/how_to/custom_agent/)
            
            *   [Custom agent](/v0.1/docs/modules/agents/how_to/custom_agent/)
            *   [Returning structured output](/v0.1/docs/modules/agents/how_to/agent_structured/)
            *   [Subscribing to events](/v0.1/docs/modules/agents/how_to/callbacks/)
            *   [Cancelling requests](/v0.1/docs/modules/agents/how_to/cancelling_requests/)
            *   [Custom LLM Agent](/v0.1/docs/modules/agents/how_to/custom_llm_agent/)
            *   [Custom LLM Agent (with a ChatModel)](/v0.1/docs/modules/agents/how_to/custom_llm_chat_agent/)
            *   [Custom MRKL agent](/v0.1/docs/modules/agents/how_to/custom_mrkl_agent/)
            *   [Handle parsing errors](/v0.1/docs/modules/agents/how_to/handle_parsing_errors/)
            *   [Access intermediate steps](/v0.1/docs/modules/agents/how_to/intermediate_steps/)
            *   [Logging and tracing](/v0.1/docs/modules/agents/how_to/logging_and_tracing/)
            *   [Cap the max number of iterations](/v0.1/docs/modules/agents/how_to/max_iterations/)
            *   [Streaming](/v0.1/docs/modules/agents/how_to/streaming/)
            *   [Timeouts for agents](/v0.1/docs/modules/agents/how_to/timeouts/)
        *   [Agents](/v0.1/docs/modules/agents/)
        *   [Tools](/v0.1/docs/modules/agents/tools/)
            
    *   [More](/v0.1/docs/modules/memory/)
        
*   [Security](/v0.1/docs/security/)
*   [Guides](/v0.1/docs/guides/)
    
*   [Ecosystem](/v0.1/docs/ecosystem/)
    
*   [LangGraph](/v0.1/docs/langgraph/)
*   * * *
    

*   [](/v0.1/)
*   [Modules](/v0.1/docs/modules/)
*   [Agents](/v0.1/docs/modules/agents/)
*   How-to
*   Access intermediate steps

Access intermediate steps
=========================

In order to get more visibility into what an agent is doing, we can also return intermediate steps. This comes in the form of an extra key in the return value.

All you need to do is initialize the AgentExecutor with `return_intermediate_steps=True`:

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 { TavilySearchResults } from "@langchain/community/tools/tavily_search";import { ChatOpenAI } from "@langchain/openai";import type { ChatPromptTemplate } from "@langchain/core/prompts";import { Calculator } from "@langchain/community/tools/calculator";import { pull } from "langchain/hub";import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";// Define the tools the agent will have access to.const tools = [new TavilySearchResults({}), new Calculator()];const llm = new ChatOpenAI({  model: "gpt-3.5-turbo-1106",  temperature: 0,});// Get the prompt to use - you can modify this!// If you want to see the prompt in full, you can at:// https://smith.langchain.com/hub/hwchase17/openai-functions-agentconst prompt = await pull<ChatPromptTemplate>(  "hwchase17/openai-functions-agent");const agent = await createOpenAIFunctionsAgent({  llm,  tools,  prompt,});const agentExecutor = new AgentExecutor({  agent,  tools,  returnIntermediateSteps: true,});const res = await agentExecutor.invoke({  input: "what is the weather in SF and then LA",});console.log(JSON.stringify(res, null, 2));/*  {    "input": "what is the weather in SF and then LA",    "output": "The current weather in San Francisco is 52°F with broken clouds. You can find more detailed information [here](https://www.timeanddate.com/weather/@5391959/historic?month=12&year=2023).\n\nThe current weather in Los Angeles is 61°F and clear. More information can be found [here](https://www.timeanddate.com/weather/usa/los-angeles/historic?month=12&year=2023).",    "intermediateSteps": [      {        "action": {          "tool": "tavily_search_results_json",          "toolInput": {            "input": "weather in San Francisco"          },          "log": "Invoking \"tavily_search_results_json\" with {\"input\":\"weather in San Francisco\"}\n",          "messageLog": [            {              "lc": 1,              "type": "constructor",              "id": [                "langchain_core",                "messages",                "AIMessage"              ],              "kwargs": {                "content": "",                "additional_kwargs": {                  "function_call": {                    "name": "tavily_search_results_json",                    "arguments": "{\"input\":\"weather in San Francisco\"}"                  }                }              }            }          ]        },        "observation": "[{\"title\":\"San Francisco, CA Hourly Weather Forecast | Weather Underground\",\"url\":\"https://www.wunderground.com/hourly/us/ca/san-francisco/date/2023-12-28\",\"content\":\"PopularCities. San Francisco, CA warning53 °F Mostly Cloudy. Manhattan, NY warning45 °F Fog. Schiller Park, IL (60176) warning53 °F Light Rain. Boston, MA warning40 °F Fog. Houston, TX 51 °F ...\",\"score\":0.9774,\"raw_content\":null},{\"title\":\"Weather in December 2023 in San Francisco, California, USA\",\"url\":\"https://www.timeanddate.com/weather/@5391959/historic?month=12&year=2023\",\"content\":\"Currently: 52 °F. Broken clouds. (Weather station: San Francisco International Airport, USA). See more current weather Select month: December 2023 Weather in San Francisco — Graph °F Sun, Dec 17 Lo:55 6 pm Hi:57 4 Mon, Dec 18 Lo:54 12 am Hi:55 7 Lo:54 6 am Hi:55 10 Lo:57 12 pm Hi:64 9 Lo:63 6 pm Hi:64 14 Tue, Dec 19 Lo:61\",\"score\":0.96322,\"raw_content\":null},{\"title\":\"2023 Weather History in San Francisco California, United States\",\"url\":\"https://weatherspark.com/h/y/557/2023/Historical-Weather-during-2023-in-San-Francisco-California-United-States\",\"content\":\"San Francisco Temperature History 2023\\nHourly Temperature in 2023 in San Francisco\\nCompare San Francisco to another city:\\nCloud Cover in 2023 in San Francisco\\nDaily Precipitation in 2023 in San Francisco\\nObserved Weather in 2023 in San Francisco\\nHours of Daylight and Twilight in 2023 in San Francisco\\nSunrise & Sunset with Twilight and Daylight Saving Time in 2023 in San Francisco\\nSolar Elevation and Azimuth in 2023 in San Francisco\\nMoon Rise, Set & Phases in 2023 in San Francisco\\nHumidity Comfort Levels in 2023 in San Francisco\\nWind Speed in 2023 in San Francisco\\nHourly Wind Speed in 2023 in San Francisco\\nHourly Wind Direction in 2023 in San Francisco\\nAtmospheric Pressure in 2023 in San Francisco\\nData Sources\\n 59.0°F\\nPrecipitation\\nNo Report\\nWind\\n0.0 mph\\nCloud Cover\\nMostly Cloudy\\n4,500 ft\\nRaw: KSFO 030656Z 00000KT 10SM FEW005 BKN045 15/12 A3028 RMK AO2 SLP253 While having the tremendous advantages of temporal and spatial completeness, these reconstructions: (1) are based on computer models that may have model-based errors, (2) are coarsely sampled on a 50 km grid and are therefore unable to reconstruct the local variations of many microclimates, and (3) have particular difficulty with the weather in some coastal areas, especially small islands.\\n We further caution that our travel scores are only as good as the data that underpin them, that weather conditions at any given location and time are unpredictable and variable, and that the definition of the scores reflects a particular set of preferences that may not agree with those of any particular reader.\\n See all nearby weather stations\\nLatest Report — 10:56 PM\\nSun, Dec 3, 2023 1 hr, 0 min ago UTC 06:56\\nCall Sign KSFO\\nTemp.\\n\",\"score\":0.94488,\"raw_content\":null},{\"title\":\"San Francisco, California December 2023 Weather Forecast - detailed\",\"url\":\"https://www.weathertab.com/en/g/o/12/united-states/california/san-francisco/\",\"content\":\"Free Long Range Weather Forecast for San Francisco, California December 2023. Detailed graphs of monthly weather forecast, temperatures, and degree days.\",\"score\":0.93142,\"raw_content\":null},{\"title\":\"Weather in San Francisco in December 2023\",\"url\":\"https://world-weather.info/forecast/usa/san_francisco/december-2023/\",\"content\":\"San Francisco Weather Forecast for December 2023 is based on long term prognosis and previous years' statistical data. 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec December Start Week On Sunday Monday Sun Mon Tue Wed Thu Fri Sat 1 +59° +54° 2 +61° +55° 3 +63° +55° 4 +63° +55° 5 +64° +54° 6 +61°\",\"score\":0.91579,\"raw_content\":null}]"      },      {        "action": {          "tool": "tavily_search_results_json",          "toolInput": {            "input": "weather in Los Angeles"          },          "log": "Invoking \"tavily_search_results_json\" with {\"input\":\"weather in Los Angeles\"}\n",          "messageLog": [            {              "lc": 1,              "type": "constructor",              "id": [                "langchain_core",                "messages",                "AIMessage"              ],              "kwargs": {                "content": "",                "additional_kwargs": {                  "function_call": {                    "name": "tavily_search_results_json",                    "arguments": "{\"input\":\"weather in Los Angeles\"}"                  }                }              }            }          ]        },        "observation": "[{\"title\":\"Weather in Los Angeles in December 2023\",\"url\":\"https://world-weather.info/forecast/usa/los_angeles/december-2023/\",\"content\":\"1 +66° +54° 2 +66° +52° 3 +66° +52° 4 +72° +55° 5 +77° +57° 6 +70° +59° 7 +66°\",\"score\":0.97811,\"raw_content\":null},{\"title\":\"Weather in December 2023 in Los Angeles, California, USA - timeanddate.com\",\"url\":\"https://www.timeanddate.com/weather/usa/los-angeles/historic?month=12&year=2023\",\"content\":\"Currently: 61 °F. Clear. (Weather station: Los Angeles / USC Campus Downtown, USA). See more current weather Select month: December 2023 Weather in Los Angeles — Graph °F Sun, Dec 10 Lo:59 6 pm Hi:61 1 Mon, Dec 11 Lo:54 12 am Hi:59 2 Lo:52 6 am Hi:72 1 Lo:63 12 pm Hi:73 0 Lo:54 6 pm Hi:59 0 Tue, Dec 12 Lo:50\",\"score\":0.96765,\"raw_content\":null},{\"title\":\"Weather in Los Angeles, December 28\",\"url\":\"https://world-weather.info/forecast/usa/los_angeles/28-december/\",\"content\":\"Weather in Los Angeles, December 28. Weather Forecast for December 28 in Los Angeles, California - temperature, wind, atmospheric pressure, humidity and precipitations. ... December 26 December 27 Select date: December 29 December 30. December 28, 2023 : Atmospheric conditions and temperature °F: RealFeel °F: Atmospheric pressure inHg: Wind ...\",\"score\":0.94103,\"raw_content\":null},{\"title\":\"Los Angeles, CA Hourly Weather Forecast | Weather Underground\",\"url\":\"https://www.wunderground.com/hourly/us/ca/los-angeles/90027/date/2023-12-28\",\"content\":\"Los Angeles Weather Forecasts. Weather Underground provides local & long-range weather forecasts, weatherreports, maps & tropical weather conditions for the Los Angeles area.\",\"score\":0.92665,\"raw_content\":null},{\"title\":\"Los Angeles, California Long Range Weather Forecast\",\"url\":\"https://www.weathertab.com/en/c/2023/12/united-states/california/los-angeles/\",\"content\":\"Los Angeles, California Long Range Weather Forecast | WeatherTAB °F °C Help United States Los Angeles, California Long Range Weather Forecast Helping You Avoid Bad Weather. 30 days and beyond. Daily ForecastDaily Calendar ForecastCalendar Detailed ForecastDetail December 2023Dec 2023\",\"score\":0.92369,\"raw_content\":null}]"      }    ]  }*/

#### API Reference:

*   [TavilySearchResults](https://api.js.langchain.com/classes/langchain_community_tools_tavily_search.TavilySearchResults.html) from `@langchain/community/tools/tavily_search`
*   [ChatOpenAI](https://api.js.langchain.com/classes/langchain_openai.ChatOpenAI.html) from `@langchain/openai`
*   [ChatPromptTemplate](https://api.js.langchain.com/classes/langchain_core_prompts.ChatPromptTemplate.html) from `@langchain/core/prompts`
*   [Calculator](https://api.js.langchain.com/classes/langchain_community_tools_calculator.Calculator.html) from `@langchain/community/tools/calculator`
*   [pull](https://api.js.langchain.com/functions/langchain_hub.pull.html) from `langchain/hub`
*   [AgentExecutor](https://api.js.langchain.com/classes/langchain_agents.AgentExecutor.html) from `langchain/agents`
*   [createOpenAIFunctionsAgent](https://api.js.langchain.com/functions/langchain_agents.createOpenAIFunctionsAgent.html) from `langchain/agents`

* * *

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

[

Previous

Handle parsing errors

](/v0.1/docs/modules/agents/how_to/handle_parsing_errors/)[

Next

Logging and tracing

](/v0.1/docs/modules/agents/how_to/logging_and_tracing/)

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.