> ## Documentation Index
> Fetch the complete documentation index at: https://composio-preview.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# After Execution Modifiers

> Transform tool results after execution

> [!NOTE]
>
> If you're building an agent, we recommend using [sessions](https://composio-preview.mintlify.site/docs/configuring-sessions.md) instead. See [Tools and toolkits](https://composio-preview.mintlify.site/docs/how-composio-works.md#meta-tools) for how sessions handle tool discovery and execution automatically.

After execution modifiers are part of Composio SDK's powerful middleware capabilities that allow you to customize and extend the behavior of tools.

These modifiers are called after the tool is executed. This allows you to modify the result of the tool before it is returned to the agent.

**Useful for:**

* Modifying or truncating the output of the tool
* Converting the output to a different format before returning it to the agent

![After Execution Modifier](https://mintcdn.com/composio-preview/8QmhU-UXaqTdWL9_/images/after-execute.png?fit=max\&auto=format\&n=8QmhU-UXaqTdWL9_\&q=85\&s=e4a2fbe259c6b78e23f26dc83a334302)

> [!NOTE]
>
> Below we use the `afterExecute` modifier to truncate the output of `HACKERNEWS_GET_USER` and only return the karma of the user.

## With Chat Completions

Since completion providers don't have a function execution step, Composio executes the tool call directly. The modifier is configured on the `tools.execute` method.

> [tab] Python
>
> ```python theme={null}
> from openai import OpenAI
> from composio import Composio, after_execute
> from composio.types import ToolExecutionResponse
> from composio_openai import OpenAIProvider
>
> openai_client = OpenAI()
> provider = OpenAIProvider()
> composio = Composio(provider=provider, toolkit_versions={"hackernews": "latest"})
> messages = [{"role": "user", "content": "What is the karma of user pg on Hacker News?"}]
>
> @after_execute(tools=["HACKERNEWS_GET_USER"])
> def after_execute_modifier(
>     tool: str,
>     toolkit: str,
>     response: ToolExecutionResponse,
> ) -> ToolExecutionResponse:
>     return {
>         **response,
>         "data": {
>             "karma": response["data"]["karma"],
>         },
>     }
>
> tools = composio.tools.get(user_id="default", slug="HACKERNEWS_GET_USER")
>
> # Get response from the LLM
> response = openai_client.chat.completions.create(
>     model="gpt-5.4",
>     tools=tools,
>     messages=messages,
> )
> print(response)
>
> # Execute the function calls
> result = provider.handle_tool_calls(
>   response=response,
>   user_id="default",
>   modifiers=[
>      after_execute_modifier,
>   ]
> )
> print(result)
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> import { Composio } from "@composio/core";
> import { OpenAI } from "openai";
>
> const openai = new OpenAI();
> const composio = new Composio({ toolkitVersions: { hackernews: "latest" } });
> const messages = [
>   { role: "user" as const, content: "What is the karma of user pg on Hacker News?" },
> ];
>
> const tools = await composio.tools.get("default", "HACKERNEWS_GET_USER");
>
> // Get response from the LLM
> const response = await openai.chat.completions.create({
>   model: "gpt-5.4",
>   tools,
>   messages,
> });
> console.log(response);
>
> // Execute the function calls
> const result = await composio.provider.handleToolCalls(
>   "default",
>   response,
>   undefined,
>   {
>     afterExecute: ({ toolSlug, toolkitSlug, result }) => {
>       if (toolSlug === "HACKERNEWS_GET_USER") {
>         return {
>           ...result,
>           data: { karma: result.data.karma },
>         };
>       }
>       return result;
>     },
>   }
> );
> console.log(result);
> ```

## With Agentic Frameworks

Agentic providers have a function execution step. The modifier is configured on the `tools.get` method which modifies the execution logic within the framework.

> [tab] Python
>
> ```python theme={null}
> from composio import Composio, after_execute
> from composio.types import ToolExecutionResponse
> from composio_crewai import CrewAIProvider
>
> composio = Composio(
>     provider=CrewAIProvider(),
>     toolkit_versions={"hackernews": "latest"},
> )
>
> @after_execute(tools=["HACKERNEWS_GET_USER"])
> def after_execute_modifier(
>     tool: str,
>     toolkit: str,
>     response: ToolExecutionResponse,
> ) -> ToolExecutionResponse:
>     return {
>         **response,
>         "data": {
>             "karma": response["data"]["karma"],
>         },
>     }
>
> tools = composio.tools.get(
>     user_id="default",
>     slug="HACKERNEWS_GET_USER",
>     modifiers=[
>         after_execute_modifier,
>     ]
> )
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> import { Composio } from "@composio/core";
> import { VercelProvider } from "@composio/vercel";
>
> const composio = new Composio({
>   apiKey: process.env.COMPOSIO_API_KEY,
>   provider: new VercelProvider(),
>   toolkitVersions: { hackernews: "latest" },
> });
>
> const userId = "default";
>
> const agenticTools = await composio.tools.get(
>   userId,
>   {
>     tools: ["HACKERNEWS_GET_USER"],
>   },
>   {
>     afterExecute: ({ toolSlug, toolkitSlug, result }) => {
>       if (toolSlug === "HACKERNEWS_GET_USER") {
>         const { karma } = result.data;
>         return {
>           ...result,
>           data: { karma },
>         };
>       }
>       return result;
>     },
>   }
> );
> ```

## Next

> [card] [Before execution modifiers](https://composio-preview.mintlify.site/docs/tools-direct/modify-tool-behavior/before-execution-modifiers.md)
>
> Modify tool arguments before execution
