← All tasks

Best LLM for Agents & Tool Use in 2026

For agents & tool use, GLM-5.2 is our pick: $2.00/M tokens on a Multi-step agent task workload, 1M context, graded 100/100 across 1 run.

Agent loops chain many model calls together, so both correctness and throughput compound — a slow or wrong step early in the loop is expensive downstream. We require a reasoning mode and weight speed almost as heavily as evidence.

What is the best LLM for agents & tool use?

GLM-5.2, from Z.ai, is the best fit for agents & tool use at $2.00 per million task tokens on a Multi-step agent task workload, with a 1M-token context window. No cheaper value pick beats it for this task.

Verified 2026-08-08
Best overall
GLM-5.2
Z.ai · $2.00/M
Fit 83/100 — the top requirements match for this task.
Best value
GLM-5.2
Z.ai · $2.00/M
The strongest fit among budget and mid-tier priced models.
Fastest
GPT-OSS 120B (Cerebras)
Cerebras · $0.43/M
2450 tokens/sec measured.
Longest context
Gemini 3.1 Pro
Google · $4.00/M
2M token context window.

Can't use GLM-5.2? See GLM-5.2 alternatives.

Evidence

Graded on 3 of 18 eligible models, run 2026-06-21. Full prompts, verbatim outputs, and grading notes below.

Median of Two Sorted Arrays in O(log n)

A classic hard algorithm: compute the median of two sorted lists in O(log(min(m,n))) time. A merge is explicitly disallowed, so the model must implement the tricky binary-search partition correctly — including empty-list and even/odd edge cases — and return code only.

Click Accuracy, Speed, or Cost to re-sort. Click any row to read the full model output.
Per-model results: accuracy, speed (tokens/sec), provider cost (USD), and the full prompt-vs-output for each model on this task.
#ModelAccuracy Speed Cost Output
Agent grade — 100/100. Passes all 5,000+ randomized cases and every edge case; genuine O(log(min(m,n))) partition search, returns a float, code-only, and explicitly raises on two empty lists. Correct — but the slowest run by far (over three minutes), and now that real usage is reported, comfortably the most expensive.
Verbatim output (152 in / 548 out tokens, 206116 ms, $0.1032)
```python
from math import inf
from typing import Sequence


def median_of_two_sorted(a: Sequence[float], b: Sequence[float]) -> float:
    if len(a) > len(b):
        a, b = b, a

    n, m = len(a), len(b)
    if n == 0 and m == 0:
        raise ValueError("median is undefined for two empty lists")

    total = n + m
    half = (total + 1) // 2

    left, right = 0, n
    while left <= right:
        i = (left + right) // 2
        j = half - i

        left_a = -inf if i == 0 else a[i - 1]
        right_a = inf if i == n else a[i]

        left_b = -inf if j == 0 else b[j - 1]
        right_b = inf if j == m else b[j]

        if left_a <= right_b and left_b <= right_a:
            if total % 2 == 1:
                return float(max(left_a, left_b))
            return float((max(left_a, left_b) + min(right_a, right_b)) / 2.0)

        if left_a > right_b:
            right = i - 1
        else:
            left = i + 1

    raise ValueError("input lists must be sorted in non-decreasing order")
```
Agent grade — 100/100. Passes all 5,000+ randomized cases and every edge case; genuine O(log(min(m,n))) partition, float return, code-only. The fastest correct solution in this task.
Verbatim output (220 in / 382 out tokens, 3790 ms, $0.01065)
```python
def median_of_two_sorted(a, b):
    # Ensure a is the smaller list
    if len(a) > len(b):
        a, b = b, a

    m, n = len(a), len(b)
    total = m + n
    half = (total + 1) // 2

    lo, hi = 0, m
    while lo <= hi:
        i = (lo + hi) // 2  # elements taken from a
        j = half - i        # elements taken from b

        a_left = a[i - 1] if i > 0 else float('-inf')
        a_right = a[i] if i < m else float('inf')
        b_left = b[j - 1] if j > 0 else float('-inf')
        b_right = b[j] if j < n else float('inf')

        if a_left <= b_right and b_left <= a_right:
            if total % 2 == 1:
                return float(max(a_left, b_left))
            else:
                return (max(a_left, b_left) + min(a_right, b_right)) / 2.0
        elif a_left > b_right:
            hi = i - 1
        else:
            lo = i + 1

    raise ValueError("Input lists are not sorted or invalid")
```
Agent grade — 100/100. Passes all 5,000+ randomized cases and every edge case with a genuine O(log) partition and float return. The <think> block ahead of the code is GLM's reasoning channel surfaced by our gateway, not reasoning dumped into the answer — GLM's actual content is the clean code block — so it scores level with the other correct solutions, as the cheapest of them.
Verbatim output (158 in / 2148 out tokens, 47713 ms, $0.009672)
<think>
We need to write a Python function `median_of_two_sorted(a, b)` that returns the median of two sorted lists in O(log(min(len(a), len(b)))) time. The function should handle empty lists, return float, and for even total return average of two middle values, for odd return the single middle value.

We must implement the classic binary search on the smaller array. The algorithm: ensure a is the smaller array. Let m = len(a), n = len(b). We want to partition a and b such that left half contains elements <= right half. We binary search on partition index i in a (0 to m). Then j = (m + n + 1) // 2 - i. We need to ensure that a[i-1] <= b[j] and b[j-1] <= a[i] (with boundary checks). If condition holds, we found the correct partition. Then if total length is odd, median is max of left half. If even, median is (max(left) + min(right)) / 2.0.

Edge cases: empty arrays. If a is empty, median is just median of b. If b is empty, median of a. But the algorithm should handle it if we set a as the smaller array. If a is empty, m=0, then i=0, j = (0+n+1)//2 = (n+1)//2. Then we need to check conditions: a[i-1] doesn't exist, so we treat as -inf. b[j-1] <= a[i] (a[i] doesn't exist, treat as inf). So condition holds. Then left max is b[j-1] (if j>0) else -inf. Right min is b[j] (if j<n) else inf. Then compute median. This works.

We must return float. For odd total, return float(median). For even, return average as float.

Implementation details: Use integer indices. Use float('inf') and float('-inf') for boundaries.

Let's write the code.

```python
def median_of_two_sorted(a, b):
    # Ensure a is the smaller array
    if len(a) > len(b):
        a, b = b, a
    m, n = len(a), len(b)
    # Binary search on a
    low, high = 0, m
    while low <= high:
        i = (low + high) // 2
        j = (m + n + 1) // 2 - i
        # Get left and right elements with boundaries
        a_left = a[i-1] if i > 0 else float('-inf')
        a_right = a[i] if i < m else float('inf')
        b_left = b[j-1] if j > 0 else float('-inf')
        b_right = b[j] if j < n else float('inf')
        
        if a_left <= b_right and b_left <= a_right:
            # Found correct partition
            if (m + n) % 2 == 1:
                return float(max(a_left, b_left))
            else:
                return (max(a_left, b_left) + min(a_right, b_right)) / 2.0
        elif a_left > b_right:
            # i is too large, need to decrease i
            high = i - 1
        else:
            # b_left > a_right, need to increase i
            low = i + 1
    # Should never reach here if inputs are sorted
    raise ValueError("Input lists are not sorted or invalid.")
```

Check: For odd total, (m+n+1)//2 gives the size of left half (larger half). For even, left half size = (m+n)//2. The formula j = (m+n+1)//2 - i works for both: if total odd, left half has one more element, so j = (total+1)//2 - i. If total even, (total+1)//2 = total//2 (since total even, total+1 odd, integer division gives total//2). So j = total//2 - i. That's correct.

Test with examples:
a = [1, 3], b = [2] -> m=2, n=1. a is larger? Actually len(a)=2, len(b)=1, so we swap: a=[2], b=[1,3]. m=1, n=2. low=0, high=1. i=0: j=(1+2+1)//2 - 0 = 4//2=2. a_left=-inf, a_right=2, b_left=b[1]=3, b_right=inf. Check: a_left <= b_right (-inf <= inf) true. b_left <= a_right? 3 <= 2 false. So b_left > a_right, need to increase i. low=1. i=1: j=2-1=1. a_left=a[0]=2, a_right=inf, b_left=b[0]=1, b_right=b[1]=3. Check: 2<=3 true, 1<=inf true. Found. total=3 odd. max(2,1)=2. return 2.0. Correct.

a = [1, 2], b = [3, 4] -> m=2, n=2. low=0, high=2. i=1: j=(4+1)//2 -1 = 2-1=1. a_left=1, a_right=2, b_left=3, b_right=4. Check: 1<=4 true, 3<=2 false. b_left > a_right, increase i. low=2. i=2: j=2-2=0. a_left=2, a_right=inf, b_left=-inf, b_right=3. Check: 2<=3 true, -inf<=inf true. Found. total=4 even. max(2, -inf)=2, min(inf, 3)=3. avg=2.5. Correct.

Empty a: a=[], b=[1,2,3]. m=0, n=3. low=0, high=0. i=0: j=(0+3+1)//2 -0 = 4//2=2. a_left=-inf, a_right=inf, b_left=b[1]=2, b_right=b[2]=3. Check: -inf<=3 true, 2<=inf true. Found. total=3 odd. max(-inf,2)=2. return 2.0. Correct.

Empty both? Not specified but if both empty, m=0,n=0. low=0, high=0. i=0: j=(0+0+1)//2 -0 = 0. a_left=-inf, a_right=inf, b_left=-inf, b_right=inf. Check: -inf<=inf true, -inf<=inf true. Found. total=0 even? (0%2==0). max(-inf,-inf)=-inf, min(inf,inf)=inf. avg = (-inf+inf)/2 = nan. But problem likely assumes at least one element? Usually median of empty is undefined. We could handle by returning 0.0 or raising error. But requirements say "Handle the case where one list is empty." It doesn't mention both empty. We can leave as is, or add a check: if m==0 and n==0: return 0.0. But to be safe, we can just let it return nan, but better to handle. Since it's not specified, we can assume at least one list non-empty. But to be robust, we can add: if m == 0 and n == 0: return 0.0. However, the problem says "Handle the case where one list is empty." So both empty might not be tested. I'll include a check for both empty returning 0.0 or maybe raise ValueError. But the instruction says "Return ONLY the code inside a single Python code block, with no explanation." So we just provide the function. I'll add a guard: if not a and not b: return 0.0. But that might be unnecessary. Let's keep it simple.

Also, we need to ensure we return float. In Python, division / returns float. For odd case, we return float(max(...)). That's fine.

Time complexity: O(log(min(m,n))) because we binary search on the smaller array. Space O(1).

Let's write the final code block.
</think>
```python
def median_of_two_sorted(a, b):
    # Ensure a is the smaller array for O(log(min(m, n))) complexity
    if len(a) > len(b):
        a, b = b, a
    m, n = len(a), len(b)
    
    # Edge case: both empty (not required, but safe)
    if m == 0 and n == 0:
        return 0.0
        
    low, high = 0, m
    while low <= high:
        i = (low + high) // 2
        j = (m + n + 1) // 2 - i
        
        a_left = a[i - 1] if i > 0 else float('-inf')
        a_right = a[i] if i < m else float('inf')
        b_left = b[j - 1] if j > 0 else float('-inf')
        b_right = b[j] if j < n else float('inf')
        
        if a_left <= b_right and b_left <= a_right:
            if (m + n) % 2 == 1:
                return float(max(a_left, b_left))
            else:
                return (max(a_left, b_left) + min(a_right, b_right)) / 2.0
        elif a_left > b_right:
            high = i - 1
        else:
            low = i + 1
            
    raise ValueError("Input lists are not sorted or invalid.")
```
Agent grade — 99/100. Passes all tests with a clean, minimal O(log) partition and float return, code-only. Docked one point only because two empty lists yield NaN rather than an explicit guard (not required by the prompt).
Verbatim output (164 in / 320 out tokens, 17413 ms, $0.004168)
```python
def median_of_two_sorted(a, b):
    if len(a) > len(b):
        a, b = b, a
        
    m, n = len(a), len(b)
    low, high = 0, m
    
    while low <= high:
        partitionA = (low + high) // 2
        partitionB = (m + n + 1) // 2 - partitionA
        
        maxLeftA = float('-inf') if partitionA == 0 else a[partitionA - 1]
        minRightA = float('inf') if partitionA == m else a[partitionA]
        
        maxLeftB = float('-inf') if partitionB == 0 else b[partitionB - 1]
        minRightB = float('inf') if partitionB == n else b[partitionB]
        
        if maxLeftA <= minRightB and maxLeftB <= minRightA:
            if (m + n) % 2 == 1:
                return float(max(maxLeftA, maxLeftB))
            else:
                return (max(maxLeftA, maxLeftB) + min(minRightA, minRightB)) / 2.0
        elif maxLeftA > minRightB:
            high = partitionA - 1
        else:
            low = partitionA + 1
```
Full test methodology →

Ranked — top 8 eligible models

"Fit" is a requirements match, not a quality benchmark — it combines price, measured speed, context window, and (where we have run it) graded accuracy on this task. Formula below.

#ModelProviderFitEvidenceTask price/MTokens/secContextScored on
1GLM-5.2Z.ai83100/1$2.001Mprice, context, evidence
2GPT-OSS 120B (Cerebras)Cerebras77$0.432450131Kprice, context, speed
3GLM 4.7 (Cerebras)Cerebras60$2.351980200Kprice, context, speed
4Gemini 3.1 ProGoogle5499/1$4.00552Mprice, context, speed, evidence
5GPT-OSS 20BGroq51$0.121120131Kprice, context, speed
6Claude Opus 4.8Anthropic47100/1$9.0058500Kprice, context, speed, evidence
7GPT-OSS 120BGroq40$0.24780131Kprice, context, speed
8Qwen 3.8 30BGroq30$1.08690131Kprice, context, speed

What this costs you

At 30,000 multi-step agent task calls/month:

ModelTask price/MEst. monthly cost
GLM-5.2$2.00$450.00
GPT-OSS 120B (Cerebras)$0.43$96.75
GLM 4.7 (Cerebras)$2.35$528.75

How we ranked this

Weights: evidence 40%, price 15%, speed 35%, context 10%.

Requirements: reasoning mode. 18 models eligible.

Price and context sub-scores are min-max normalised (log-scaled) within this task's eligible set only. Speed uses measured tokens/sec only — estimated rows are excluded. A model missing a measurement is never scored as zero: its weight is redistributed across the components we do have, and "Scored on" in the table above shows exactly which ones.

Prices verified 2026-08-08, accuracy graded 2026-06-21.

Related

Z.ai provider hubGLM-5.2 pricingBest LLM for CodingBest LLM for Math & ReasoningBest LLM for Chatbots & Support

FAQ

Why is speed weighted so heavily for agents?

An agent loop is many sequential model calls, not one — a model that is 2x faster finishes a 10-step loop in roughly half the wall-clock time.

Do I need a reasoning model for tool-use agents?

For anything beyond simple single-tool calls, yes — planning which tool to call next and interpreting its output benefits directly from an explicit reasoning mode.

Is our agents evidence the same as a full agent benchmark?

No — it is one algorithmic coding task graded for correctness, used as a proxy for step-level reasoning quality. Treat it as a signal, not a full agentic-benchmark score.

Run this exact prompt against the top 3

Don't take a ranking's word for it — try GLM-5.2 and its closest alternatives on your own prompt.

Try It Free