Skip to main content

LangChain integration for PactSpec — discover and invoke verified AI agents as LangChain tools

Project description

pactspec-langchain

LangChain integration for PactSpec -- discover and invoke verified AI agents as LangChain tools with automatic pricing awareness.

Each skill in a PactSpec agent becomes a LangChain BaseTool with its name, description, input schema, and pricing information derived from the spec. The LLM sees pricing in the tool description and can make cost-aware decisions.

Installation

pip install pactspec-langchain

Quick start

from pactspec_langchain import PactSpecToolkit

# Discover verified agents for a task
toolkit = PactSpecToolkit.from_registry(
    query="invoice processing",
    verified_only=True,
    max_price=0.10,
)

# List what was found
for tool in toolkit.get_tools():
    print(f"{tool.name}: {tool.description}")

Using with a LangChain agent

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.agents import create_tool_calling_agent, AgentExecutor
from pactspec_langchain import PactSpecToolkit

# 1. Discover tools from the registry
toolkit = PactSpecToolkit.from_registry(
    query="document analysis",
    verified_only=True,
    max_price=0.25,
)

# 2. Set up the LLM and prompt
llm = ChatOpenAI(model="gpt-4o")
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant. Use the available tools to help the user. "
               "Pay attention to tool costs and prefer cheaper options when quality is similar."),
    ("human", "{input}"),
    MessagesPlaceholder("agent_scratchpad"),
])

# 3. Create the agent
agent = create_tool_calling_agent(llm, toolkit.get_tools(), prompt)
executor = AgentExecutor(agent=agent, tools=toolkit.get_tools(), verbose=True)

# 4. Run it
result = executor.invoke({"input": "Extract line items from this invoice: ..."})
print(result["output"])

Load a specific agent

If you know the exact agent you want, load it by spec ID:

toolkit = PactSpecToolkit.from_agent(
    "urn:pactspec:acme/invoice-agent@1.0.0",
    auth_headers={"Authorization": "Bearer sk-your-api-key"},
)

Filtering

By price

# Only tools that cost at most $0.05 per invocation
toolkit = PactSpecToolkit.from_registry(
    query="translation",
    max_price=0.05,
)

By pricing model

# Only free tools
toolkit = PactSpecToolkit.from_registry(
    query="summarization",
    pricing_model="free",
)

# Only per-token pricing
toolkit = PactSpecToolkit.from_registry(
    query="summarization",
    pricing_model="per-token",
)

Verified agents only

# Only agents that have passed PactSpec verification
toolkit = PactSpecToolkit.from_registry(
    query="code review",
    verified_only=True,
)

Combining filters

toolkit = PactSpecToolkit.from_registry(
    query="medical coding",
    verified_only=True,
    max_price=0.10,
    pricing_model="per-invocation",
)

How pricing appears in tool descriptions

Each tool's description includes pricing and verification status so the LLM can reason about costs:

Extract line items from invoices | Cost: 0.05 USD/per-invocation via stripe | [Verified] | Agent: InvoiceBot v2.1.0

For free tools:

Summarize text documents | Cost: Free | Agent: SummaryAgent v1.0.0

Authentication

Pass headers for authenticated agent endpoints:

toolkit = PactSpecToolkit.from_registry(
    query="invoice processing",
    auth_headers={
        "Authorization": "Bearer sk-your-key",
        "X-API-Key": "your-api-key",
    },
)

Using pre-fetched agents

If you've already fetched agents via the PactSpec Python SDK, avoid a second network call:

from pactspec import PactSpecClient
from pactspec_langchain import PactSpecToolkit

client = PactSpecClient()
result = client.search(q="translation", verified_only=True)

# Pass agent records directly
toolkit = PactSpecToolkit.from_agents(
    result.agents,
    max_price=0.10,
)

API reference

PactSpecToolkit

Method Description
from_registry(query, ...) Search the registry and create tools from matching agents
from_agent(spec_id, ...) Create tools from a specific agent by spec ID or UUID
from_agents(agents, ...) Create tools from pre-fetched AgentRecord objects
get_tools() Return all tools (standard LangChain toolkit interface)
get_tool(name) Look up a single tool by name
tool_names List of all tool names

PactSpecTool

Extends langchain_core.tools.BaseTool. Each instance wraps a single PactSpec agent skill.

Attribute Description
agent_meta AgentMetadata with agent ID, endpoint, verified status
skill_meta SkillMetadata with skill ID, description, pricing, input schema
timeout HTTP timeout in seconds (default: 30)
auth_headers Extra headers sent with every invocation

License

MIT

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

pactspec_langchain-0.1.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

pactspec_langchain-0.1.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file pactspec_langchain-0.1.0.tar.gz.

File metadata

  • Download URL: pactspec_langchain-0.1.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for pactspec_langchain-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7a238e56d1ee3f4dcf6d4ad57a14eac21f089bd9af8575a45e666bf2a15397d5
MD5 6ac4365a8c4eff1447100dab861cdc19
BLAKE2b-256 90c4cbbe16117e4d4b310d5a3c66516f9d29fbd3d0513fc18d47cb81056d113c

See more details on using hashes here.

File details

Details for the file pactspec_langchain-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pactspec_langchain-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5f16b6f23e2d89c340b9400910680f5e1b10be3740fe868060bd905902288b42
MD5 7b21336b415035f2d7aed34fc1dc3656
BLAKE2b-256 800961d4d57bb9cc91ab28be684382d68f458fade937695d33c0ae8038ed7760

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