4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AgentCoreッ!よくもクオータをッ! ◯してやるぞ、◯してやるーッ!!

Last updated at Posted at 2025-08-21

気分はディリータです

image.png

ミスった Agent 起動、どうやって止めるのよ、という話

AgentCore でホストしている Agent に適切なプロンプトを入れられずに実行しちゃった場合、Bedrock のクオータを浪費することになります。
佐々木朗希のストレートのような速度で終わる Agent じゃない場合は佐々木浪費みたいなトークンを無駄遣いすることになります。次のエージェントを実行するにもクオータが…

そんなとき動いている Agent を止められるのか、というお話

コード

こんなエージェント…ではなくトイコードを作ってみました

main.py
import asyncio
from bedrock_agentcore import BedrockAgentCoreApp

app = BedrockAgentCoreApp()
current_task = None

@app.async_task
async def background_work():
    for i in range(3600):
        print(f"{i+1}")
        await asyncio.sleep(1)
    return "Game Over"

@app.entrypoint
async def handler(event):
    global current_task
    
    if "prompt" in event:
        print("ガハハハ Bedrock Quota を食い尽くしてやる!!")
        current_task = asyncio.create_task(background_work())
        return {"status": "started"}
    
    if "kill" in event and current_task:
        print("◯してやるーー!!!")
        current_task.cancel()
        current_task = None
        print("◯してやったぜ!!!")
        return {"status": "killed"}

if __name__ == "__main__":
    app.run()

非同期処理(処理を止めたくなるような長時間処理なら非同期でしょう)で動かす前提において、動いているタスクを global 変数(current_task) で記憶しておき、kill というキーのリクエストがきたら current_task をキャンセルします。

本当に止まったかを確認するため、非同期処理は1秒毎にカウントアップしたのを print(=CWLに書き込み)していき、止めなければ 3600秒=1時間動かし続けてゲームオーバーです。

AgentCore の発火は以下のとおりです。

import boto3
import json

client = boto3.client('bedrock-agentcore')
input_text = json.dumps({"prompt":"test"})

response = client.invoke_agent_runtime(
    agentRuntimeArn="<your agentcore runtime arn",
    payload=input_text
)
print(response)

これでカウントアップがはじまります。

そして、止めるときのコードはこちら

response = client.invoke_agent_runtime(
    agentRuntimeArn="<your agentcore runtime arn",,
    runtimeSessionId = response["runtimeSessionId"],
    payload=json.dumps({"kill":"kill"})
)
print(response)

さて、ログを見てみましょう。


2025-08-21T08:55:52.348Z
INFO: Started server process [1]
2025-08-21T08:55:52.348Z
INFO: Waiting for application startup.
2025-08-21T08:55:52.348Z
INFO: Application startup complete.
2025-08-21T08:55:52.348Z
INFO: Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
2025-08-21T08:55:52.520Z
INFO: 127.0.0.1:36582 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:26.226Z
ガハハハ Bedrock Quota を食い尽くしてやる!!
2025-08-21T08:56:26.226Z
INFO: 127.0.0.1:36590 - "POST /invocations HTTP/1.1" 200 OK
2025-08-21T08:56:26.226Z
2025-08-21 08:56:26,224 - bedrock_agentcore.app - INFO - [3beb8561] Invocation completed successfully (0.000s)
2025-08-21T08:56:26.226Z
2025-08-21 08:56:26,225 - bedrock_agentcore.app - INFO - [3beb8561] Async task started: background_work (ID: 8876725457114168676)
2025-08-21T08:56:27.883Z
1
2025-08-21T08:56:27.883Z
2
2025-08-21T08:56:27.883Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:29.883Z
3
2025-08-21T08:56:29.883Z
4
2025-08-21T08:56:29.883Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:31.884Z
5
2025-08-21T08:56:31.884Z
6
2025-08-21T08:56:31.884Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:33.886Z
7
2025-08-21T08:56:33.886Z
8
2025-08-21T08:56:33.886Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:35.887Z
9
2025-08-21T08:56:35.887Z
10
2025-08-21T08:56:35.887Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:37.668Z
2025-08-21 08:56:37,668 - bedrock_agentcore.app - INFO - [a27b9ef9] Invocation completed successfully (0.000s)
2025-08-21T08:56:37.668Z
11
2025-08-21T08:56:37.668Z
12
2025-08-21T08:56:37.668Z
◯してやるーー!!!
2025-08-21T08:56:37.668Z
◯してやったぜ!!!
2025-08-21T08:56:37.668Z
INFO: 127.0.0.1:58342 - "POST /invocations HTTP/1.1" 200 OK
2025-08-21T08:56:37.669Z
2025-08-21 08:56:37,669 - bedrock_agentcore.app - INFO - [3beb8561] Async task completed: background_work (ID: 8876725457114168676, Duration: 11.44s)
2025-08-21T08:56:37.888Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:39.889Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:41.890Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:43.891Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:45.895Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:47.893Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:49.894Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:51.895Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK
2025-08-21T08:56:53.896Z
INFO: 127.0.0.1:36594 - "GET /ping HTTP/1.1" 200 OK

カウントアップ、そしてカウントアップの停止が確認できましたね!

まとめ

global 変数でタスクを定義しておけばどこからでも◯せます。
安心してください。◯せますから。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?