Skip to content
Blog

Long-Running Tools: Managing Asynchronous Jobs and Polling Patterns

Handle jobs that outlive a turn with LongRunningFunctionTool — pause semantics, event.long_running_tool_ids, resuming with FunctionResponse, and when to poll instead.

Published on • November 28, 2026

AI Assistant

Some operations do not fit inside a model turn: a report export takes four minutes, a manager’s approval takes a day, a third-party job runs on someone else’s schedule. Blocking the tool until it finishes burns your execution deadline; returning a fake “done” lies to the model. ADK’s answer is LongRunningFunctionTool: the tool starts the work, returns an initial result, and the framework deliberately pauses the agent run so your client can wait, poll, and later send the outcome back as a new message.

This tutorial covers the pause/resume mechanics, how the client detects pending calls through event.long_running_tool_ids, the exact shape of the resume message, and how to choose between a long-running tool, an async tool, and an external job you poll.

The contract: start, don’t wait

from typing import Any
from google.adk.tools import LongRunningFunctionTool

def submit_export(report_id: str, format: str) -> dict[str, Any]:
    """Submit an asynchronous report export job.

    Args:
        report_id (str): Report to export, format 'R-12345'.
        format (str): Output format, 'csv' or 'pdf'.

    Returns:
        dict with 'status' ('pending' while running) and 'job_id'.
    """
    job_id = jobs.create(report_id=report_id, format=format)
    return {"status": "pending", "job_id": job_id,
            "summary": f"Export {job_id} started for report {report_id}."}

export_tool = LongRunningFunctionTool(func=submit_export)

Wrap semantics: your function starts the operation and returns immediately with an initial result — typically an operation ID and status: 'pending'. The framework packages that into a FunctionResponse, delivers it to the model so it can tell the user “export started, id X”, and then the agent run ends/pauses. Nothing is left running inside the runner.

The docs are blunt about the boundary: this tool is for starting and managing long tasks, not performing them. The actual work belongs in a separate worker, queue, or service.

Detecting the pause in your client

Events carry a long_running_tool_ids set — the IDs of function calls awaiting an external response. Your client captures the call, does the waiting (polling the job API, watching a queue, waiting for a human), and ships the result back:

async def run_turn(query: str):
    content = types.Content(role="user", parts=[types.Part(text=query)])
    pending_call, pending_response = None, None

    async for event in runner.run_async(
        session_id=session.id, user_id=USER_ID, new_message=content
    ):
        if event.long_running_tool_ids and event.content and event.content.parts:
            for part in event.content.parts:
                if part.function_call and part.function_call.id in event.long_running_tool_ids:
                    pending_call = part.function_call
        if pending_call and event.content and event.content.parts:
            for part in event.content.parts:
                if part.function_response and part.function_response.id == pending_call.id:
                    pending_response = part.function_response
        if event.is_final_response() and event.content:
            print(event.content.parts[0].text)   # "Export started, id job_91"

    if pending_response:
        job_id = pending_response.response["job_id"]
        result = await poll_until_done(job_id)    # your own polling loop

        updated = pending_response.model_copy(deep=True)
        updated.response = {"status": "completed", "artifact_url": result.url}

        async for event in runner.run_async(
            session_id=session.id, user_id=USER_ID,
            new_message=types.Content(
                role="user",
                parts=[types.Part(function_response=updated)],
            ),
        ):
            if event.content and event.content.parts:
                print(event.content.parts[0].text)   # "Export ready: ..."

The resume message is a FunctionResponse part replayed as a user-turn message with the same function-call ID — that’s what stitches it to the original call. Sending an intermediate response (status: 'running', progress: 0.4) lets the agent announce progress before the final one.

If the agent uses the Resume feature, the long-running response must also carry the invocation_id of the invocation that made the request — otherwise the system treats it as a new invocation entirely.

Three patterns compared

Async function toolLongRunningFunctionToolExternal job + polling tool
Where work runsInside the turn, awaitedStarted by tool, completed outsideFully outside; tool only queries
Turn behaviourBlocks until all parallel calls finishPauses after the initial resultCompletes every turn with a status
Duration ceilingTurn/deadline limitsUnbounded — waits are your client’s businessUnbounded
Progress reportingOnly at the endIntermediate FunctionResponse updatesEvery poll can report
Client responsibilityNoneMust capture long_running_tool_ids and resumeMust poll and decide when to stop
FitsI/O-bound lookups, fan-outApproval flows, exports, provisioningContinuous pipelines, external CI

Choose by who owns the wait: parallel I/O belongs in async function tools; human or system waits belong in the long-running tool; continuous background work belongs outside the agent where you run headless jobs.

Polling design rules

When your client owns the wait, it owns the failure modes too:

  • Bound the loop. Cap attempts and wall-clock time; a stuck job must reach a terminal status: 'error' response rather than polling forever (timeouts and deadlines).
  • Back off. Exponential backoff with jitter — fixed-interval polling hammers the job API and gets you rate-limited (429 handling).
  • Store the job ID in session state (tool_context.state["pending_export"] = job_id) so a fresh turn or a second process can find it, rather than threading it through conversation text.
  • Be idempotent. Resumes can be replayed; a job-status query must be safe to run twice.
  • Distinguish pending from failed. An intermediate update saying “still running” and a final one saying “failed: report deleted” are different messages — encode that in the status field the way error contracts dictate.

Keeping the user informed

While the run is paused, the model has said nothing more — your UI is the channel. Show the pending call and its ID immediately, surface each intermediate FunctionResponse as a progress update, and only re-enter the agent loop when you have something worth narrating. Sessions persist events, so a resumed run’s history is auditable: the initial call, each progress response, and the final outcome are all events in the session.

Takeaways

  • LongRunningFunctionTool starts work and returns an initial result; the framework pauses the run and your client decides when to resume — it never executes the long task itself.
  • Detect pending calls via event.long_running_tool_ids, match responses by function-call ID, and resume by sending a FunctionResponse part (plus invocation_id when using Resume).
  • Intermediate responses are progress updates; the final response closes the loop and lets the agent produce the user-facing answer.
  • Pick the pattern by who owns the wait: async tool for in-turn I/O, long-running tool for waits your client controls, external jobs for continuous work.
  • Bound every poll: deadline, backoff, idempotent status reads, and the job ID in session state.

Further reading