Skip to main content

LlamaIndex Agent Integration: Coa

Chain-of-Abstraction Agent Pack

pip install llama-index-agent-coa

The chain-of-abstraction (CoA) agent integration implements a generalized version of the strategy described in the origin CoA paper.

By prompting the LLM to write function calls in a chain-of-thought format, we can execute both simple and complex combinations of function calls needed to execute a task.

The LLM is prompted to write a response containing function calls, for example, a CoA plan might look like:

After buying the apples, Sally has [FUNC add(3, 2) = y1] apples.
Then, the wizard casts a spell to multiply the number of apples by 3,
resulting in [FUNC multiply(y1, 3) = y2] apples.

From there, the function calls can be parsed into a dependency graph, and executed.

Then, the values in the CoA are replaced with their actual results.

As an extension to the original paper, we also run the LLM a final time, to rewrite the response in a more readable and user-friendly way.

NOTE: In the original paper, the authors fine-tuned an LLM specifically for this, and also for specific functions and datasets. As such, only capabale LLMs (OpenAI, Anthropic, etc.) will be (hopefully) reliable for this without finetuning.

Code Usage

pip install llama-index-agent-coa

First, setup some tools (could be function tools, query engines, etc.)

from llama_index.core.tools import FunctionTool


def add(a: float, b: float) -> float:
    """Add two numbers together."""
    return a + b


def multiply(a: float, b: float) -> float:
    """Multiply two numbers together."""
    return a * b


add_tool = FunctionTool.from_defaults(fn=add)
multiply_tool = FunctionTool.from_defaults(fn=multiply)

Next, create the pack with the tools, and run it!

from llama_index.agent.coa import CoAAgentWorker
from llama_index.llms.openai import OpenAI

agent = CoAAgentWorker(
    llm=OpenAI(model="gpt-4.1-mini"),
    tools=[add_tool, multiply_tool],
).as_agent()

resp = agent.chat("What is 123.123*101.101 and what is its product with 12345")

Metadata

Release files for llama-index-agent-coa 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llama-index-agent-coa 0.3.3
File Size Uploaded
llama_index_agent_coa-0.3.3.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for llama-index-agent-coa 0.3.3
File Interpreter ABI Platform
llama_index_agent_coa-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 18.2 kB

Release files / llama_index_agent_coa-0.3.3.tar.gz

Download URL llama_index_agent_coa-0.3.3.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c293bf21ccadfe935ca04cf22cbc34fe1bef002979de67860207df4b2a177035
BLAKE2b-256 checksum
How to use checksums
3b1e91add84c54ac73674044a700546b6a4e611dd6687c79be44eade0fb86755
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.16

Release files / llama_index_agent_coa-0.3.3-py3-none-any.whl

Download URL llama_index_agent_coa-0.3.3-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eb96f4f5c48bf0a4452e624b35b927e6701a0dcfabab551974b75b404143afed
BLAKE2b-256 checksum
How to use checksums
51330c839548f2002a9a8650ad4bba0ba0b54734aaf9292e75a9eab4e70ce477
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.16

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page