Skip to main content
Use LithoBlocks with CrewAI so your agents can send Slack messages via templates. An agent (or a dedicated “notifier” agent) uses a tool to call the LithoBlocks API with a template ID and payload. You get deterministic messages and far fewer tokens because the LLM does not generate Block Kit JSON. The resulting LLM savings help offset LithoBlocks cost, with net cost reduction at higher volumes.

Prerequisites

  • LithoBlocks API key and at least one template
  • Connecting Slack for your LithoBlocks workspace
  • CrewAI installed (pip install crewai or equivalent)

Why use LithoBlocks with CrewAI

  • Deterministic messages — The agent picks a template and data; the tool sends it. No Block Kit in the prompt.
  • Lower LLM cost — The agent outputs only template_id and a small payload; the tool does the HTTP call. LLM savings help offset LithoBlocks cost; at higher volumes, net cost reduction.
  • Consistency — Same layout every time; fewer malformed blocks. A dedicated “Notifier” agent can own the tool and send to Slack on behalf of other agents.

Implementing the tool

Define a CrewAI tool that accepts template_id and payload, then POSTs to the LithoBlocks API.
import os
import requests
from crewai.tools import tool

LITHOBLOCKS_API_KEY = os.environ.get("LITHOBLOCKS_API_KEY")
BASE_URL = "https://api.lithoblocks.com"

@tool("Send Slack message")
def send_slack_message(template_id: str, payload: dict) -> str:
    """Send a Slack message using a LithoBlocks template. Use when the user or another agent wants to notify someone in Slack.
    template_id: The LithoBlocks template ID (e.g. from your dashboard).
    payload: JSON object matching the template's placeholders (e.g. {"user": {"name": "Alice"}, "message": "Hello"}).
    """
    url = f"{BASE_URL}/templates/{template_id}/send"
    headers = {
        "Authorization": f"Bearer {LITHOBLOCKS_API_KEY}",
        "Content-Type": "application/json",
    }
    response = requests.post(url, json=payload, headers=headers)
    if response.ok:
        return f"Message sent successfully: {response.json().get('message_ts', 'ok')}"
    return f"Failed to send message: {response.status_code} {response.text}"
For compile (get blocks without sending), use POST /templates/{template_id}/compile. See the API reference for request/response shapes.

Using the tool in an agent or task

Assign the tool to an agent (e.g. “Notifier” or “Researcher”). The agent can send Slack messages by calling the tool with template_id and payload.
from crewai import Agent, Task, Crew

notifier = Agent(
    role="Notifier",
    goal="Send Slack messages when the user or other agents need to notify someone.",
    backstory="You use LithoBlocks templates to send structured Slack messages.",
    tools=[send_slack_message],
    verbose=True,
)

task = Task(
    description="Send a Slack message to #general saying the report is ready.",
    agent=notifier,
)

crew = Crew(agents=[notifier], tasks=[task])
result = crew.kickoff()
The agent will call send_slack_message with the appropriate template_id and payload for your template.

Next steps