!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

*   [Use cases](/v0.1/docs/use_cases/)
    
    *   [SQL](/v0.1/docs/use_cases/sql/)
        
    *   [Chatbots](/v0.1/docs/use_cases/chatbots/)
        
    *   [Extraction](/v0.1/docs/use_cases/extraction/)
        
    *   [Query Analysis](/v0.1/docs/use_cases/query_analysis/)
        
        *   [Quickstart](/v0.1/docs/use_cases/query_analysis/quickstart/)
        *   [Techniques](/v0.1/docs/use_cases/query_analysis/techniques/decomposition/)
            
            *   [Decomposition](/v0.1/docs/use_cases/query_analysis/techniques/decomposition/)
            *   [Expansion](/v0.1/docs/use_cases/query_analysis/techniques/expansion/)
            *   [Hypothetical Document Embeddings](/v0.1/docs/use_cases/query_analysis/techniques/hyde/)
            *   [Routing](/v0.1/docs/use_cases/query_analysis/techniques/routing/)
            *   [Step Back Prompting](/v0.1/docs/use_cases/query_analysis/techniques/step_back/)
            *   [Structuring](/v0.1/docs/use_cases/query_analysis/techniques/structuring/)
        *   [How-To Guides](/v0.1/docs/use_cases/query_analysis/how_to/few_shot/)
            
    *   [Q&A with RAG](/v0.1/docs/use_cases/question_answering/)
        
    *   [Tool use](/v0.1/docs/use_cases/tool_use/)
        
    *   [Interacting with APIs](/v0.1/docs/use_cases/api/)
    *   [Tabular Question Answering](/v0.1/docs/use_cases/tabular/)
    *   [Graphs](/v0.1/docs/use_cases/graph/)
        
    *   [Summarization](/v0.1/docs/use_cases/summarization/)
    *   [Agent Simulations](/v0.1/docs/use_cases/agent_simulations/)
        
    *   [Autonomous Agents](/v0.1/docs/use_cases/autonomous_agents/)
        
    *   [Code Understanding](/v0.1/docs/use_cases/code_understanding/)
    *   [Audio/Video Structured Extraction](/v0.1/docs/use_cases/media/)

*   [](/v0.1/)
*   [Use cases](/v0.1/docs/use_cases/)
*   [Query Analysis](/v0.1/docs/use_cases/query_analysis/)
*   Techniques
*   Step Back Prompting

On this page

Step Back Prompting
===================

Sometimes search quality and model generations can be tripped up by the specifics of a question. One way to handle this is to first generate a more abstract, “step back” question and to query based on both the original and step back question.

For example, if we ask a question of the form “Why does my LangGraph agent streamEvents return {LONG\_TRACE} instead of {DESIRED\_OUTPUT}” we will likely retrieve more relevant documents if we search with the more generic question “How does streamEvents work with a LangGraph agent” than if we search with the specific user question.

Let’s take a look at how we might use step back prompting in the context of our Q&A bot over the LangChain YouTube videos.

Setup[​](#setup "Direct link to Setup")
---------------------------------------

#### Install dependencies[​](#install-dependencies "Direct link to Install dependencies")

tip

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

*   npm
*   yarn
*   pnpm

    npm i @langchain/core zod

    yarn add @langchain/core zod

    pnpm add @langchain/core zod

#### Set environment variables[​](#set-environment-variables "Direct link to Set environment variables")

    # Optional, use LangSmith for best-in-class observabilityLANGSMITH_API_KEY=your-api-keyLANGCHAIN_TRACING_V2=true

Step back question generation[​](#step-back-question-generation "Direct link to Step back question generation")
---------------------------------------------------------------------------------------------------------------

Generating good step back questions comes down to writing a good prompt:

### Pick your chat model:

*   OpenAI
*   Anthropic
*   FireworksAI
*   MistralAI

#### Install dependencies

tip

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

*   npm
*   yarn
*   pnpm

    npm i @langchain/openai 

    yarn add @langchain/openai 

    pnpm add @langchain/openai 

#### Add environment variables

    OPENAI_API_KEY=your-api-key

#### Instantiate the model

    import { ChatOpenAI } from "@langchain/openai";const llm = new ChatOpenAI({  model: "gpt-3.5-turbo-0125",  temperature: 0});

#### Install dependencies

tip

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

*   npm
*   yarn
*   pnpm

    npm i @langchain/anthropic 

    yarn add @langchain/anthropic 

    pnpm add @langchain/anthropic 

#### Add environment variables

    ANTHROPIC_API_KEY=your-api-key

#### Instantiate the model

    import { ChatAnthropic } from "@langchain/anthropic";const llm = new ChatAnthropic({  model: "claude-3-sonnet-20240229",  temperature: 0});

#### Install dependencies

tip

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

*   npm
*   yarn
*   pnpm

    npm i @langchain/community 

    yarn add @langchain/community 

    pnpm add @langchain/community 

#### Add environment variables

    FIREWORKS_API_KEY=your-api-key

#### Instantiate the model

    import { ChatFireworks } from "@langchain/community/chat_models/fireworks";const llm = new ChatFireworks({  model: "accounts/fireworks/models/firefunction-v1",  temperature: 0});

#### Install dependencies

tip

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

*   npm
*   yarn
*   pnpm

    npm i @langchain/mistralai 

    yarn add @langchain/mistralai 

    pnpm add @langchain/mistralai 

#### Add environment variables

    MISTRAL_API_KEY=your-api-key

#### Instantiate the model

    import { ChatMistralAI } from "@langchain/mistralai";const llm = new ChatMistralAI({  model: "mistral-large-latest",  temperature: 0});

    import { StringOutputParser } from "@langchain/core/output_parsers";import { ChatPromptTemplate } from "@langchain/core/prompts";const system = `You are an expert at taking a specific question and extracting a more generic question that gets at \the underlying principles needed to answer the specific question.You will be asked about a set of software for building LLM-powered applications called LangChain, LangGraph, LangServe, and LangSmith.LangChain is a Python framework that provides a large set of integrations that can easily be composed to build LLM applications.LangGraph is a Python package built on top of LangChain that makes it easy to build stateful, multi-actor LLM applications.LangServe is a Python package built on top of LangChain that makes it easy to deploy a LangChain application as a REST API.LangSmith is a platform that makes it easy to trace and test LLM applications.Given a specific user question about one or more of these products, write a more generic question that needs to be answered in order to answer the specific question. \If you don't recognize a word or acronym to not try to rewrite it.Write concise questions.`;const prompt = ChatPromptTemplate.fromMessages([  ["system", system],  ["human", "{question}"],]);const stepBack = prompt.pipe(llm).pipe(new StringOutputParser());

    const question = `I built a LangGraph agent using Gemini Pro and tools like vectorstores and duckduckgo search.How do I get just the LLM calls from the event stream`;const result = await stepBack.invoke({ question: question });console.log(result);

    What are the specific methods or functions within LangGraph that allow for filtering or extracting LLM calls from an event stream?

Returning the stepback question and the original question[​](#returning-the-stepback-question-and-the-original-question "Direct link to Returning the stepback question and the original question")
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

To increase our recall we’ll likely want to retrieve documents based on both the step back question and the original question. We can easily return both like so:

    import { RunnablePassthrough } from "@langchain/core/runnables";const stepBackAndOriginal = RunnablePassthrough.assign({ stepBack });await stepBackAndOriginal.invoke({ question: question });

    {  question: "I built a LangGraph agent using Gemini Pro and tools like vectorstores and duckduckgo search.\n" +    "How do"... 47 more characters,  stepBack: "What is the process for extracting specific types of calls, such as LLM calls, from an event stream "... 37 more characters}

Using function-calling to get structured output[​](#using-function-calling-to-get-structured-output "Direct link to Using function-calling to get structured output")
---------------------------------------------------------------------------------------------------------------------------------------------------------------------

If we were composing this technique with other query analysis techniques, we’d likely be using function calling to get out structured query objects. We can use function-calling for step back prompting like so:

    import { z } from "zod";const stepBackQuerySchema = z.object({  stepBackQuestion: z    .string()    .describe(      "Given a specific user question about one or more of these products, write a more generic question that needs to be answered in order to answer the specific question."    ),});const llmWithTools = llm.withStructuredOutput(stepBackQuerySchema, {  name: "StepBackQuery",});const hydeChain = prompt.pipe(llmWithTools);await hydeChain.invoke({ question: question });

    {  stepBackQuestion: "What are the steps involved in extracting specific types of calls from an event stream in a software"... 13 more characters}

* * *

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

[

Previous

Routing

](/v0.1/docs/use_cases/query_analysis/techniques/routing/)[

Next

Structuring

](/v0.1/docs/use_cases/query_analysis/techniques/structuring/)

*   [Setup](#setup)
*   [Step back question generation](#step-back-question-generation)
*   [Returning the stepback question and the original question](#returning-the-stepback-question-and-the-original-question)
*   [Using function-calling to get structured output](#using-function-calling-to-get-structured-output)

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.