Skip to content

Dynamic Prompts Middleware

Context-aware dynamic prompt generation.

Configuration

  • File: src/chatbot/agentic/middlewares/create_dynamic_prompts_middlewares.py

Available Middlewares

create_dynamic_prompt_for_career_chat_middleware

Generates dynamic prompts for the career chat agent with user context.

create_dynamic_prompt_for_opportunities_suggestion_middleware

Creates context-aware prompts for opportunity suggestions.

create_dynamic_prompt_for_task_helper_middleware

Generates prompts for the task helper with user information.

create_dynamic_prompt_for_talent_finder_agent_suggestion_middleware

Creates prompts for talent finder with search context.

How It Works

  1. Middleware intercepts agent request
  2. Retrieves user data from database
  3. Builds system instructions from application and tenant-owned configuration
  4. Adds summaries and existing preferences as labeled HumanMessage data
  5. Applies the shared untrusted-context policy before the model call

Example

from langchain.agents import create_agent

from src.chatbot.agentic.middlewares.create_dynamic_prompts_middlewares import (
    create_dynamic_prompt_for_opportunities_suggestion_middleware,
)

agent = create_agent(
    model=llm,
    tools=tools,
    middleware=[create_dynamic_prompt_for_opportunities_suggestion_middleware],
)

Benefits

  • Personalized responses - Agent knows user context
  • Better recommendations - Uses profile data
  • Reduced hallucinations - Grounded in real data
  • Token efficiency - Only relevant context included

Stable instructions are placed before request-specific context. Long shared prefixes can therefore use provider prompt caching; short prompts are left uncached. Persisted conversation summaries are supplied as user-role data without duplicating an existing synthetic summary message. Preferences use the same boundary through UntrustedContextPromptMiddleware. Neither is elevated to system instructions. See Shared input security.