async def finalize(
        self, ctx: Context, output: AgentOutput, memory: BaseMemory
    ) -> AgentOutput:
        """
        Finalize the function calling agent.

        Adds all in-progress messages to memory.
        """
        scratchpad: List[ChatMessage] = await ctx.get(self.scratchpad_key, default=[])
        await memory.aput_messages(scratchpad)

        # reset scratchpad
        await ctx.set(self.scratchpad_key, [])

        return output

        Finalize the function calling agent.

        Adds all in-progress messages to memory.
        
llama-index-core/llama_index/core/agent/workflow/function_agent.py