Skip to main content

Agentic AI out of the box

Project description

Cogency (Python)

Multi-step reasoning agents with clean DX

Installation

pip install cogency

Quick Start

from cogency.agent import Agent
from cogency.llm import GeminiLLM, KeyRotator
from cogency.tools.calculator import CalculatorTool

# Setup LLM with key rotation
keys = ["key1", "key2", "key3"]
key_rotator = KeyRotator(keys)
llm = GeminiLLM(key_rotator=key_rotator)

# Create agent with tools
agent = Agent(
    name="MathAgent", 
    llm=llm, 
    tools=[CalculatorTool()]
)

# Run with tracing
result = agent.run("What is 15 * 23?", enable_trace=True)
print("Response:", result["response"])

Core Architecture

Cogency uses a clean 5-step reasoning loop:

  1. Plan - Decide strategy and if tools are needed
  2. Reason - Select tools and prepare inputs
  3. Act - Execute tools with validation
  4. Reflect - Evaluate results and decide next steps
  5. Respond - Format clean answer for user

Built-in Tools

Tools are auto-discovered from the /tools/ directory:

  • CalculatorTool - Basic arithmetic operations
  • WebSearchTool - Web search using DuckDuckGo

Adding Custom Tools

Create a new tool by extending the BaseTool base class:

from cogency.tools.base import BaseTool

class WeatherTool(BaseTool):
    def __init__(self):
        super().__init__(
            name="weather",
            description="Get current weather for a location"
        )
    
    def run(self, location: str) -> dict:
        # Your implementation here
        return {"temperature": 72, "condition": "sunny"}

Drop the file in /tools/ and it's automatically available.

LLM Support

Gemini

from cogency.llm import GeminiLLM

llm = GeminiLLM(api_key="your-key")
# or with key rotation
llm = GeminiLLM(key_rotator=KeyRotator(["key1", "key2"]))

OpenAI (coming soon)

from cogency.llm import OpenAILLM

llm = OpenAILLM(api_key="your-key")

Tracing

Enable detailed execution tracing to debug your agents:

result = agent.run("Complex task", enable_trace=True)

if "execution_trace" in result:
    from cogency.utils.formatting import format_trace
    print(format_trace(result["execution_trace"]))

Example trace output:

--- Execution Trace (ID: abc123) ---
PLAN     | Need to calculate complex math problem
REASON   | TOOL_CALL: calculator(operation='multiply', num1=15, num2=23)
ACT      | calculator -> {'result': 345}
REFLECT  | Calculation completed successfully
RESPOND  | 15 multiplied by 23 equals 345.
--- End Trace ---

Error Handling

All tools include built-in validation and error handling:

# Invalid tool calls are caught and reported
result = agent.run("Calculate abc + def")
# Returns clean error message instead of crashing

CLI Usage

Run the example from command line:

cd python
poetry run python ../examples/basic_usage.py

Configuration

Agents are configured through simple constructor parameters:

agent = Agent(
    name="MyAgent",           # Agent identifier
    llm=llm,                 # LLM instance
    tools=[tool1, tool2],    # List of available tools
    max_iterations=10        # Optional: limit reasoning loops
)

Development

Running Tests

poetry run pytest

Adding New LLMs

Extend the BaseLLM class and implement required methods:

from cogency.llm import GeminiLLM

class YourLLM(GeminiLLM):
    def generate(self, prompt: str, **kwargs) -> str:
        # Your implementation
        pass

Roadmap

  • ✅ Multi-step reasoning loop
  • ✅ Tool auto-discovery
  • ✅ Clean execution tracing
  • ✅ Error handling and validation
  • 🔄 OpenAI + Anthropic LLM support
  • 🔄 Memory and persistence
  • 🔄 Multi-agent coordination
  • 🔄 Streaming responses

License

MIT License - see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cogency-0.2.0.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cogency-0.2.0-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file cogency-0.2.0.tar.gz.

File metadata

  • Download URL: cogency-0.2.0.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.10 Darwin/24.5.0

File hashes

Hashes for cogency-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c9a09e96b622592834faf9aaba4ce0875a8bdd3609901777442b67312357583b
MD5 bda8eb7c519d22edc28879323be710f2
BLAKE2b-256 066864b253f7c03da0253c42cf302d278e7fa503f5f74e208654bb09d46060e7

See more details on using hashes here.

File details

Details for the file cogency-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: cogency-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.10 Darwin/24.5.0

File hashes

Hashes for cogency-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1f57b6c2cdb9a7ba01dfd177f4468aea5b91225e3867f0fbfa1690c4987a3216
MD5 79bbf84bdc6b5abe2220e54508eaff4c
BLAKE2b-256 f4ee2de329155d43f66a6dec0e5cdc17236b335b993692c9b6bb97e6ec328e57

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page