10 Go Time Patterns for Agent Scheduling

· Medium ·

1 min read Original article ↗

Timeouts, retries, and graceful shutdown — all in Go’s time package.

Cheikh seck

Agent loops are full of timing questions. How long did that tool call take? What if it hangs? Should we retry? When should the next step run? How do we shut down cleanly?

Go’s time package is the standard library’s home for measuring, representing, and acting on time. It gives you a set of functions to help you achieve this. For agents, it's relevant because agents are asynchronous and time-bound. They call tools, call models, retry failed requests, schedule follow-ups, and shut down. The time package provides the building blocks to do that without rolling your own clock or channel logic.

Press enter or click to view image in full size

1. Measure how long something takes

Before an agent step calls a tool or model, you record a start time. After it finishes, you compute the elapsed duration. This gives you latency data for logs, metrics, and traces.

start := time.Now()
// ... agent does work ...
elapsed := time.Since(start)

In the benchmark, the agent step took 21.8ms. That number turns “the agent took time” into something you can log, plot, and compare.

2. Don’t let a slow task block everything