async def generate_reply_with_instructions(
    session: AgentSession[UserData], instructions: str
) -> None:
    """
    Generate a reply with ephemeral instructions.

    - Moves base prompt to developer role (position 0, high priority)
    - Flushes agent._instructions
    - Uses instructions parameter for ephemeral (system role)
    - Restores everything after

    This ensures base prompt has highest priority via developer role.
    """

    # Switch base prompt to developer role for higher priority
    logger.debug("Using developer role for base, system for ephemeral")

    original_instructions = agent._instructions
    base_instruction_id = "base.developer.instruction"
    system_instructions_id = "lk.agent_task.instructions"

    # Remove existing system instruction if present
    system_idx = agent._chat_ctx.index_by_id(system_instructions_id)
    removed_system_msg = None
    if system_idx is not None:
        removed_system_msg = agent._chat_ctx.items.pop(system_idx)

    # Insert base as developer at position 0
    agent._chat_ctx.items.insert(
        0,
        llm.ChatMessage(
            id=base_instruction_id,
            role="developer",
            content=[original_instructions],
        ),
    )

    # Flush agent instructions so LiveKit doesn't manage them
    agent._instructions = ""

    try:
        # Generate reply with ephemeral instructions (will be system role)
        speech = session.generate_reply(
            instructions=instructions,
            tool_choice="none",
        )
        await speech.wait_for_playout()
    finally:
        # Restore original state
        if msg := agent._chat_ctx.get_by_id(base_instruction_id):
            agent._chat_ctx.items.remove(msg)

        agent._instructions = original_instructions

        # Restore system message at position 0 (after developer is removed)
        # Don't use stale system_idx as context may have changed
        if removed_system_msg is not None:
            agent._chat_ctx.items.insert(0, removed_system_msg)

        logger.debug("Restored original instruction state")