Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
docs: clean up Cloudflare Worker sessions after requests
Brendan O'Leary committed
Sep 22, 2026 at 15:34 UTC
4d59f5db03883ce9b52fe67e5e2a0b5dd0207990
1 file changed
+8
-2
docs/content/examples/cloudflare-workers.mdx
+8
-2
@@ -99,10 +99,11 @@ export default {
99
apiKey: env.COMPOSIO_API_KEY,
100
provider: new OpenAIResponsesProvider(),
101
});
102
+ let session: Awaited<ReturnType<typeof composio.create>> | undefined;
103
104
try {
105
const openai = new OpenAI({ apiKey: env.OPENAI_API_KEY });
105
- const session = await composio.create('hackernews-worker', {
106
+ session = await composio.create('hackernews-worker', {
107
toolkits: ['hackernews'],
108
manageConnections: false,
109
sandbox: { enable: false },
@@ -141,6 +142,11 @@ export default {
142
console.error('Hacker News agent failed', error);
143
return new Response('The agent could not complete the request.', { status: 502 });
144
} finally {
145
+ try {
146
+ await session?.delete();
147
+ } catch (error) {
148
+ console.error('Could not delete the Composio session', error);
149
+ }
150
ctx.waitUntil(composio.flush());
151
}
152
},
@@ -149,7 +155,7 @@ export default {
155
156
`handleToolCalls` executes each tool through the session that supplied it. The loop sends the results back to OpenAI and stops when the model returns text, with a limit of ten model turns. Connection management and the remote sandbox are disabled because this task only reads public Hacker News data.
157
152
-The handler awaits the agent result before returning. `ctx.waitUntil(composio.flush())` lets Composio finish sending telemetry after the response. Cloudflare gives [`waitUntil` up to 30 seconds](https://developers.cloudflare.com/workers/runtime-apis/context/#waituntil) after the response or a client disconnect, so keep the agent run in the awaited request path.
158
+The handler awaits the agent result and deletes the request's session before returning, including when the agent fails. If deletion fails, it logs the error and preserves the response. `ctx.waitUntil(composio.flush())` lets Composio finish sending telemetry after the response. Cloudflare gives [`waitUntil` up to 30 seconds](https://developers.cloudflare.com/workers/runtime-apis/context/#waituntil) after the response or a client disconnect, so keep the agent run in the awaited request path.
159
160
## Check and run locally
161